0

I have a input field with id 'datepicker'

While validating, if date mentioned is not valid

I am empty the field and adding the placeholder

 if(validationfails)
 {
    $("#datepicker").val(''); 
    $("#datepicker").attr('placeholder',"MM/DD/YYYY")
 }

Now the input field doesn't have anything, but the placeholder is missing.

if I focus the input field and press "backspace"

The place holder "MM/DD/YYYY" appears

Karthik Amar
  • 217
  • 5
  • 17

1 Answers1

0

Try to change your code to this one. Enclose your IDs in quotes.

            if(validationfails)
        {
            $('#datepicker').val('');
            $('#datepicker').attr('placeholder',"MM/DD/YYYY")
        }
rashidkhan
  • 462
  • 8
  • 24
  • Thannks for replying., But sorry rashid, that was typo – Karthik Amar Jun 15 '15 at 09:32
  • It's working right? I checked it on my local machine. – rashidkhan Jun 15 '15 at 09:35
  • Multiple things can cause this. (1) You are not including jQuery properly (2) Try to include all your code in jQuery ready function as in my code (3) if validationfails variable returns false then the code will not be executed so try to add `!validationfails`. My code looks like this. `(function($) { $(document).ready(function() { validationfails = true; if(validationfails) { $('#datepicker').val(''); $('#datepicker').attr('placeholder',"MM/DD/YYYY") } }); })(jQuery);` – rashidkhan Jun 15 '15 at 10:30