Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a title from textbox clicked on & Numeric values to ignore numeric type #270

Closed
DisaTemp opened this issue May 19, 2014 · 3 comments

Comments

@DisaTemp
Copy link

I am hoping to achieve two things. First, I would like to be able to when I click on a textbox, show the label...maybe from an alt tag of what I clicked on. For example, if I clicked on a text box that is of units, I could show Units as a title to they keyboard.

The second thing I am trying to accomplish is if I have units, to ignore them on the numeric pad. Ex: Text Box contains "15 inches". When numeric keyboard opens have it only show numbers 15...say I change to 12, but on Accept pre-pend the "inches" back in as the value.

Thoughts?

@Mottie
Copy link
Owner

Mottie commented May 26, 2014

Hi @mappenzellar!

Sorry I have been responding to your emails and forgot to update the issue. Basically, the hidden and visible callbacks can be used to add/remove any labels (demo):

HTML (include a data-unit attribute with the unit of measure)

<input class="keyboard" type="text" data-unit="km" value="100 km">

Script

$(function(){
    $('.keyboard').keyboard({
        layout: 'num',
        hidden : function(e, keyboard, el) {
            var unit = $(el).attr('data-unit');
            $(el).val(function(indx, val){
                if (val.replace(/[^\d,. \-()]/g, '') === '') {
                    return '';
                } else if (/\w/.test(val)){
                    return $.trim( val.replace(/[^\d,. \-()]/g, '') ) + ' ' + unit;
                } else {
                    return val + ' ' + unit;
                }
            });
        },
        visible: function(e, keyboard, el) {
            keyboard.$preview.val(function(indx, val){
                return $.trim( val.replace(/[^\d,. \-()]/g, '') );
            });
            keyboard.$preview[0].select();
        }
    })
});

@Mottie
Copy link
Owner

Mottie commented May 26, 2014

Oops, I forgot to add the Label to the keyboard (demo):

This is the only change needed to the above code:

visible: function (e, keyboard, el) {
        var unit = $(el).attr('data-unit');
        keyboard.$preview.val(function (indx, val) {
                return $.trim(val.replace(/[^\d,. \-()]/g, ""));
        });
        keyboard.$keyboard.prepend('<h2>' + unit + '</h2>');
        keyboard.$preview[0].select();
}

with a little css to center the title:

.ui-keyboard h2 {
    text-align: center;
    font-weight: bold;
}

@Mottie
Copy link
Owner

Mottie commented Aug 23, 2014

I'm guessing this issue has been resolved, so I'm going to close it. If you continue to have problems, please feel free to continue this discussion.

@Mottie Mottie closed this as completed Aug 23, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants