Search code examples
javascriptinternet-explorer-8internet-explorer-9placeholder

Placeholder IE9 - Javascript not executed in IE9


I'm currently developping a website in Drupal and i used a Javascript to replace the placeholder property in IE8-9. Here's the code :

$('input[placeholder]').focus(function() {
  var input = $(this);
  if (input.val() == input.attr('placeholder')) {
    input.val('');
    input.removeClass('placeholder');
  }
}).blur(function() {
  var input = $(this);
  if (input.val() == '' || input.val() == input.attr('placeholder')) {
    input.addClass('placeholder');
    input.val(input.attr('placeholder'));
  }
}).blur();

But it doesn't seem to be executed. The navigator doesn't go inside the function. When i launch it trough the console it works fine. Does anyone have an idea of how to fix it ?

EDIT : Even when putting the right selector, it's still not working Thanks a lot


Solution

  • The placeholder is an attribute of some html elements (inputs), you have to add a selector matching the given attribute:

    $('*[placeholder]').focus(function() { //Or input[placeholder]
      var input = $(this);
      if (input.val() == input.attr('placeholder')) {
        input.val('');
        input.removeClass('placeholder');
      }
    }).blur(function() {
      var input = $(this);
      if (input.val() == '' || input.val() == input.attr('placeholder')) {
        input.addClass('placeholder');
        input.val(input.attr('placeholder'));
      }
    }).blur();