JavaScript select an css class for jquery.inputmask

2.5k views Asked by At

Hi for my script i must use jquery.inputmask but I have a problem with the input field selection. I can't use ids (because of other script) so I tried this:

var FormComponents = function () {

    var handleInputMasks = function () {
        $.extend($.inputmask.defaults, {
            'autounmask': true
        });

        $(":input").inputmask("99999.99999.99999", {placeholder:" ", clearMaskOnLostFocus: true }); //default
    }

    return {
        //main function to initiate the module
        init: function () {
            handleInputMasks();
        }

    };

}();  

All work but now all inputs have the inputmask. But I only want to have the input with the css class inputmask.

So I tried

$(".inputmask input[type=text]").inputmask("99999.99999.99999", {placeholder:" ", clearMaskOnLostFocus: true }); //default

and

$(".inputmask :input").inputmask("99999.99999.99999", {placeholder:" ", clearMaskOnLostFocus: true }); //default

but both are not working. Anybody an idea?

3

There are 3 answers

0
davidethell On

Put your class after your element selector:

$("input.inputmask").inputmask("99999.99999.99999", {placeholder:" ", clearMaskOnLostFocus: true }); //default

or else just use the class with no element:

$(".inputmask").inputmask("99999.99999.99999", {placeholder:" ", clearMaskOnLostFocus: true }); //default
0
Jakub Kotrs On

If the input itself has the class, then one of these:

$('input.inputmask');
$('.inputmask');

should be better. Using a space in the selector implies a child, so for example

$('.inputmask input[type="text"]')

selects an input[type="text"] inside .insputmask.

1
TrivoXx On

Ok I think I must post the code to understand. Because I think I have explain first something a little bit false.

<div class="inputmask" style="position: relative;">
<input type="text" class="input-medium" style="padding-right: 24px;">
<span class="editable-clear-x" style="display: block;"></span>
</div>

I can't use the class="input-medium" because it is an class its used in other input. So I must use inputmask.