How to test if the browser supports the native placeholder attribute?

Jannis picture Jannis · Oct 14, 2010 · Viewed 20.9k times · Source

I'm trying to write a simple placeholder jQuery plugin for a site of mine but of course I only want to fire the function if the native placeholder attribute isn't supported…

How can I use jQuery to test for native support of the placeholder attribute?

Answer

lonesomeday picture lonesomeday · Oct 14, 2010

You can add to $.support quite easily by inserting this at the top of the Javascript you've written:

jQuery.support.placeholder = (function(){
    var i = document.createElement('input');
    return 'placeholder' in i;
})();

You can then use either $.support.placeholder or jQuery.support.placeholder anywhere in your code.

NB This code adapted from diveintohtml5, the link provided by hellvinz above.