Thursday, November 27, 2014

Placeholder attribute for IE Hack



HTML5 introduced the "placeholder" attribute on input elements, which allows to display a greyed-out default text. But Sadly the Internet Explorer, including IE 9 does not support it. I got a solution. Just try this.


if(navigator.appVersion.match(/MSIE [\d.]+/)){

    var placeholder = $('input[type=text]').attr('placeholder');
    var placeholderText = placeholder;


    $('input[type=text]').val(placeholderText);
    $('input[type=text]').blur(function(){
        $(this).val() == '' ? $(this).val(placeholderText) : false;
    });


    $('input[type=text]').focus(function(){
        $(this).val() == placeholderText ? $(this).val('') : false;
    });
}