JQuery HTML5 placeholder fix

Wednesday, March 17th, 2010

One of the nifty new features of HTML5 is the placeholder for forms which is added by putting in a placeholder value.

<input type="email" placeholder="example@kamikazemusic.com" />

So I can use this everywhere I’ve just written a quick bit of jQuery to replicate the placeholder functionality in browser that don’t support. It also uses Modernizr to check if the browser already supports the placeholder technique.

To use this make sure you’ve included the jQuery and Modernizr files and then pop in the bit of code below or download and include the file.
(This is only tested so far with Modernizr v1.1 and jQuery v1.4.2)

jQuery placeholder [size : 446 bytes]


$(document).ready(function() {

if(!Modernizr.input.placeholder){


$("input").each(
function(){
if($(this).val()=="" && $(this).attr("placeholder")!=""){
$(this).val($(this).attr("placeholder"));
$(this).focus(function(){
if($(this).val()==$(this).attr("placeholder")) $(this).val("");
});
$(this).blur(function(){
if($(this).val()=="") $(this).val($(this).attr("placeholder"));
});
}
});

}


});

Tags: , , ,

Filed under Quick Tips.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
14 Responses to “JQuery HTML5 placeholder fix”
  1. NK says:

    gr8 information

  2. Tim B. says:

    Without jQuery:

    function initInputPlaceholders() {
    function onFocus() {
    if(this.value == this.getAttribute(‘placeholder’)) {
    this.value = ”;
    }
    }
    function onBlur() {
    if(this.value == ”) {
    this.value = this.getAttribute(‘placeholder’);
    }
    }
    var inputs = document.getElementsByTagName(‘input’)
    , i, l = inputs.length
    , input;
    for(i = 0; i < l; i += 1) {
    input = inputs[i];
    if(input.value == '' && input.hasAttribute('placeholder')) {
    input.value = input.getAttribute('placeholder');
    input.addEventListener('focus', onFocus, false);
    input.addEventListener('blur', onBlur, false);
    }
    }
    }

    just call the function on load or on domready
    note: does not work in IE

  3. better says:

    This works better since it handles clearing the placeholder on submit

    $(document).ready(function() {
    if (Modernizr.input.placeholder)
    return;

    $(‘input[placeholder]‘).focus(function() {
    if ($(this).hasClass(‘placeholder’)) {
    if ($(this).val() == $(this).attr(‘placeholder’))
    $(this).val(”);
    $(this).removeClass(‘placeholder’);
    }
    });

    $(‘input[placeholder]‘).keypress(function() {
    if ($(this).hasClass(‘placeholder’)) {
    if ($(this).val() == $(this).attr(‘placeholder’))
    $(this).val(”);
    $(this).removeClass(‘placeholder’);
    }
    });

    $(‘input[placeholder]‘).blur(function() {
    if ($(this).val() != ”)
    return;
    $(this).addClass(‘placeholder’);
    $(this).val($(this).attr(‘placeholder’));
    });

    $(‘input[placeholder]‘).each(function() {
    if ($(this).val() != ” && $(this).val() != $(this).attr(‘placeholder’))
    return;
    $(this).val($(this).attr(‘placeholder’)).addClass(‘placeholder’);
    });

    $(‘form’).submit(function() {
    $(this).find(“.placeholder”).each(function() {
    $(this).removeClass(‘placeholder’);
    $(this).val(”);
    });
    });
    });

  4. qq says:

    if(this.value == this.getAttribute(‘placeholder’)) {
    this.value = ”;
    }
    }
    function onBlur() {
    if(this.value == ”) {
    this.value = this.getAttribute(‘placeholder’);

Pingbacks and Trackbacks
Leave a Reply


CommentLuv Enabled

RSS FeedFollow me on twitter - @dsparks83



Advertisments

heart internet WooThemes - WordPress themes for everyone

Tags

analytics, blogging, CSS, CSS3, design, development, ecommerce, Flash, google, google analytics, guest post, HTML, HTML5, HTML email, IE6, Javascript, randomiser interview, six revisions, Web Design, XHTML