How to use .unmask() function on MaskedInput jQuery app?

8.3k Views Asked by At

I try to use .unmask() function after .blur() (jQuery), but it did not worked.

Please help, this is my code:

$('.cnpj').focus(function()
{
    $(this).mask('99999999/9999-99');
})
.blur(function(e)
{
   if($(this).val().length == 0)
   {
        var mask = '99999999/9999-99';
        $(this).unmask(mask);
        $(this).val('CNPJ');
   }
});

Here is the HTML code:

<input type="text" id="cnpj" name="cnpj" class="cnpj empty entriesFormInput01" value="CNPJ" />

My goal is that when the user don't put any value in the input, the input return with the same value='CNPJ', because I have a lot of input in the form and the label is the value, if the user focus in one input and leave, the input will be empty if no value and i want to, when he leaves the input without a value the first value came back again.

1

There are 1 best solutions below

2
On BEST ANSWER

First of all I think that jQuery mask() function doesn't exists as default. There is an plugin for jQuery which can be used like that, is that right? I've found this plugin. I think your code didn't worked because you didn't load this plugin or another one.

$('.cnpj').focus(function(){
    if($(this).val() == 'CNPJ') 
       $(this).val('');
    //$(this).mask('99999999/9999-99');
}).blur(function(){
    if($(this).val().length == 0)
    {
        //var mask = '99999999/9999-99';
        //$(this).unmask(mask);
        $(this).val('CNPJ');
     }
});