Quantcast
Channel: atomiku » Web Development
Viewing all articles
Browse latest Browse all 10

jQuery HTML5 Placeholder Input Fallback (for browsers that do not support the placeholder attribute)

$
0
0

Hey, placeholder is a really useful HTML5 feature but unfortunately not all browsers support it. So here is a small jQuery script that will replicate placeholder functionality for older browsers. Read more for the code.

Here is an input with a placeholder attribute:

And here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$(document).ready(function(){
    $('[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();
    $('[placeholder]').parents('form').submit(function() {
        $(this).find('[placeholder]').each(function() {
            var input = $(this);
            if (input.val() == input.attr('placeholder')) {
                input.val('');
            }
        })
    });
});

The post jQuery HTML5 Placeholder Input Fallback (for browsers that do not support the placeholder attribute) appeared first on atomiku.


Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles



Latest Images