camainc
camainc

Reputation: 3810

How can I work around the jQuery MaskedInput plugin's different selected text behavior?

Sorry for the long question title, but I wasn't sure of the best way to word it :-(

The issue is that the excellent Masked Input jQuery plugin by Josh Bush selects the text in an input box on focus when the mask is fixed length, but it forces the caret to the end of the field when the mask has an optional character in it.

If you go to his demo page (http://digitalbush.com/projects/masked-input-plugin/) you can see this in action.

Fill in some values in the Date, the Phone, and the Phone + Ext fields, then tab through them. You will notice that the Date and Phone fields are fully selected when the fields receive the focus, but when you tab into the Phone + Ext field, the caret goes to the end of the field.

I would like the behavior to be the same for all masked fields (select all text, like in the first two examples above), regardless of whether there is an optional mask character.

Has anyone else worked around this issue, and if so, could you please post how you did it?

Thanks in advance.!

Fixed-length masks select all text like so... Masks with optional characters force the caret to the end of the field.

Upvotes: 1

Views: 1722

Answers (1)

Tom Regan
Tom Regan

Reputation: 3841

If you don't mind altering jquery.maskedinput-1.3.1.js, substitute this bit of code starting at line 308:

caretTimeoutId = setTimeout(function(){
                writeBuffer();
                var trimmedMask = mask.replace('?','');
                if (pos == trimmedMask.length) {
                    input.caret(0, pos);
                } else {
                    input.caret(pos);
                }
            }, 10);

In the minified file you need to substitute this at character 3075:

var t2=t.replace('?','');b(),e==t2.length

I found your question after posting a question about the same issue: https://stackoverflow.com/questions/19794942/jquery-masked-input-will-not-select-on-focus-when-mask-contains-optional-operato

Upvotes: 1

Related Questions