HTML5 Placeholder in IE and Unsupported Browsers

December 9th, 2011

The html5 placeholder attribute on an input tag is a really nice, however easily adding that feature to older IE versions and any other browsers that don’t support it renders it unusable in production.

The below javascript/jquery code will check the page for any inputs with the placeholder attribute and make it work in browsers that do not support it, and it will do nothing in those that do.

(function($) {
  $.fn.placeholder = function() {
    if(typeof document.createElement("input").placeholder == 'undefined') {
      $('[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().parents('form').submit(function() {
        $(this).find('[placeholder]').each(function() {
          var input = $(this);
          if (input.val() == input.attr('placeholder')) {
            input.val('');
          }
        })
      });
    }
  }
})(jQuery);

Line 3 of the javascript checks if the browser supports the placeholder attribute and only runs if it does not.

To call this plugin just add the following to your page.

$.fn.placeholder();

You may also want to add the following css to make it more like the browsers that do support it.

.placeholder { color: #aaa; }

The original bulk of this code was written by Nico Hagenburger. I basically only added the support check and turned it into a plugin.

7 Responses to "HTML5 Placeholder in IE and Unsupported Browsers"

  1. Hey,

    This one helped me.

    It’s a shame IE lacks these great HTML5 functionality additions.

    All the best,

    Leniel

  2. Apichai says:

    Hello Cory,

    Thank you very much for this great code. You saved me!

    regards,
    Apichai

  3. SID says:

    Hello,

    Thanks very much for this js function.
    Now IE again have a sense! :)

    Cheers man,
    SID

  4. David says:

    Dude, you are the bomb! Helped me on Stack Overflow, and I’ve really been working on this for 4 hours. You know your stuff and you are willing to help others. Thank You!

  5. CoryMathews says:

    No problem david. Glad to help.

  6. sg3s says:

    Shouldn’t .parents(‘form’) really be .closest(‘form’) ?

  7. CoryMathews says:

    @sg3s I had not even seen .closest() before you mentioned it. It seems to be a better fit, but I will have to give it a test first.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>