Printable Version of Topic

Click here to view this topic in its original format

Forums _ Webmasters' Corner _ jQuery Default Search Value

Posted by: Mikeplyts Jul 4 2010, 01:29 AM

So, I'm messing with jQuery right now, and I'm trying to do that ol' default search value trick. I have this so far:

CODE
$(function() {
    $('.text').click(function() {
        if ($('.text').val() == '') {
            $('.text').val('Search');
            $('.text').focus(function() {
                if ($('.text').val() == 'Search') {
                    $('.text').val('');
                }
            });
            $('.text').blur(function() {
                if ($('.text').val() == '') {
                    $('.text').val('Search');
                }
            });
        }
    });
});


It seems kind of glitchy, though. Like, it won't load the default value, but when you click on the input field, it loads the default value, and if you click out of it (blur), it stays the same, click again (focus) and it disappears, click out and it stays vanished. Any ideas? Simpler methods could also be useful.