Created
October 29, 2011 09:16
-
-
Save comfuture/1324263 to your computer and use it in GitHub Desktop.
emulate placeholder property to browser that didn't support placeholder property it self
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
jQuery(function() { | |
jQuery.support.placeholder = 'placeholder' in document.createElement('input'); | |
if (jQuery.support.placeholder) return; | |
var isEmpty = function(el) { | |
return el.val().length === 0 || el.val() == el.attr('placeholder'); | |
}, | |
hold = function(el) { | |
if (isEmpty(el)) { | |
var e = el.get(0); | |
/* // disabled feature cause of restrict to set type of password input | |
if (e.tagName == 'INUPUT' && el.attr('type') == 'password') { | |
el.attr('data-type', 'password'); | |
e.type = 'text'; | |
} | |
*/ | |
el.val(el.attr('placeholder')).addClass('-no-value'); | |
} | |
}; | |
jQuery('[placeholder]').focus(function(event) { | |
var el = jQuery(this); | |
/* // disabled feature cause of restrict to set type of password input | |
if (el.attr('data-type')) { | |
el.get(0).type = el.attr('data-type'); | |
} | |
*/ | |
if (isEmpty(el)) { | |
el.val('').removeClass('-no-value'); | |
} | |
}).blur(function(event) { | |
var el = jQuery(this); | |
if (el.val().length === 0) { | |
hold(el); | |
} | |
}).each(function(i, e) { | |
var el = jQuery(e); | |
hold(el); | |
}); | |
jQuery('form').submit(function(event) { | |
jQuery(this).find('[placeholder]').each(function(i, e) { | |
var el = jQuery(e); | |
if (el.val() === el.attr('placeholder')) { | |
el.val(''); | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment