JQuery applies an input mask to the onfocus field and removes onblur to avoid problems with placeholder text

I have a date of birth field:

<input type="text" id="dob" name="dob" placeholder="Date of Birth" />

I use the jQuery plugin (Charl van Niekerk placeholder text) to make sure older browsers see placeholder text. All is good so far.

When I am clicked on the date of birth field, I want to switch from the placeholder text to the input mask. For this, I use another plugin (http://digitalbush.com/projects/masked-input-plugin/).

However, I only want to activate the onfocus input mask and remove its onblur, otherwise it stops showing placeholder text.

I need something like:

$(document).ready(function() {    
    $('#dob').focus(function() {  
        $.mask.definitions['~']='[+-]';
        $('#dob').mask('99/99/9999');  
    });
    $('#dob').blur(function() {
        // Something here to unmask?   
    });  
});

This code is currently not working. Any ideas?

+3
source share
1 answer

, ():

(function($) {
  $(document).ready(function() {
    $('#dob').focus(function() {
      $.mask.definitions['~']='[+-]';
      $(this).mask('99/99/9999');
    }).blur(function() {
      $(this).unmask();
    });
  });
})(jQuery);

, (function ($) {}) (jQuery). , .

+4

Source: https://habr.com/ru/post/1788183/


All Articles