HTML 5: Placeholder

The placeholder attribute shows text  or short hint in a field intended to help user in data entry. The text will be disappear until the field is focused upon, then hides the text. Placeholder text is supported by Firefox 3.7+ and Chrome/Safari 4.0+
Example:
<input type="text" name="FirstName" value="" placeholder="First Name">
Output will be:


Now, when IE and Opera is being used your placeholder won't work. To work for your placeholder we need to do some javascript. 




/* This will check if your browser supports placeholder */
jQuery(function() {
jQuery.support.placeholder = false;
test = document.createElement('input');
if('placeholder' in test) jQuery.support.placeholder = true;
});

/* This will transfer your placeholder to your input value */

$(function() {
if(!$.support.placeholder) {
var active = document.activeElement;
$(':text').focus(function () {
if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
$(this).val('').removeClass('hasPlaceholder');
}
}).blur(function () {
if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
}
});
$(':text').blur();
$(active).focus();
$('form').submit(function () {
$(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
});
}
});

Comments

Popular posts from this blog

Basketball Hand Signals

Uhaw ang Tigang na Lupa- Liwayway A. Arceo

White Dots