Created
September 14, 2011 11:05
-
-
Save liwh/1216319 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This assumes you're using jQuery. This does the heavy lifting. | |
function setPlaceholderText() { | |
var PLACEHOLDER_SUPPORTED = 'placeholder' in document.createElement('input'); | |
if (PLACEHOLDER_SUPPORTED || !$(':input[placeholder]').length) { | |
return; | |
} | |
$(':input[placeholder]').each(function() { | |
var el = $(this); | |
var text = el.attr('placeholder'); | |
function add_placeholder() { | |
if (!el.val() || el.val() === text) { | |
el.val(text).addClass('placeholder_text'); | |
} | |
} | |
add_placeholder(); | |
el.focus(function() { | |
if (el.val() === text) { | |
el.val('').removeClass('placeholder_text');; | |
} | |
}).blur(function() { | |
if (!el.val()) { | |
el.val(text).addClass('placeholder_text');; | |
} | |
}); | |
el.closest('form').submit(function() { | |
if (el.val() === text) { | |
el.val(''); | |
} | |
}).bind('reset', function() { | |
setTimeout(add_placeholder, 50); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment