-
-
Save hagenburger/379601 to your computer and use it in GitHub Desktop.
// Released under MIT license: http://www.opensource.org/licenses/mit-license.php | |
$('[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().parents('form').submit(function() { | |
$(this).find('[placeholder]').each(function() { | |
var input = $(this); | |
if (input.val() == input.attr('placeholder')) { | |
input.val(''); | |
} | |
}) | |
}); |
I wrote a "better" version (conflict free, more performant, on document ready)
and a hand-minified version of it
Here’s a much more robust jQuery plugin that detects native support and only polyfills when necessary: http://mths.be/placeholder
Is there anyway to clear the form when using onchange to submit the form?
hey! there is a small defect in Chrome (for Windows at least) — after focus event happened, the placeholder text is still shown in the input field, and it has no placeholder style.
Great snippet !
I'm using it with the placeholder feature detection:
placeholderSupport = ("placeholder" in document.createElement("input"));
if(!placeholderSupport ){
//your code here
}
Nice Fix, it integrated pretty smooth.
coljung thanks for the feature detection!
Lovely work! Thanks!
For some reason, it still doesn't work in IE9.
also, placeholders on http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html don't work in ie9 either...
But There is a problem with while using placeholder for text area.What to do?
Any way to get it work on a password field?
Thank you very much, hagenburger
Not working for hidden fields on the page that become visible later. via JQuery/JavaScript code. Any help?
Threw Coljung's detection and some strict comparison into another gist:
https://gist.github.com/jayseeg/6998790
Thanks for the gist!
You saved my time! Great gist. Thanks for that!
He. It's the good simple but I find bug and fix it(if you enter placeholder value to input, text is retire):
var isInputSupported = 'placeholder' in document.createElement('input');
var isTextareaSupported = 'placeholder' in document.createElement('textarea');
if (!isInputSupported || !isTextareaSupported) {
$('[placeholder]').focus(function () {
var input = $(this);
if (input.val() == input.attr('placeholder') && input.data('placeholder')) {
input.val('');
input.removeClass('placeholder');
}
}).blur(function () {
var input = $(this);
if (input.val() == '') {
input.addClass('placeholder');
input.val(input.attr('placeholder'));
input.data('placeholder', true);
} else {
input.data('placeholder', false);
}
}).blur().parents('form').submit(function () {
$(this).find('[placeholder]').each(function () {
var input = $(this);
if (input.val() == input.attr('placeholder') && input.data('placeholder')) {
input.val('');
}
})
});
}
I tried this script and for some reason when I load the page the placeholder is missing but when I click and then click off of the input the placeholder is injected in. Any ideas? The site is local so I don't have a link unfortunately.
@daebat just do:
setTimeout(function(){
$('[placeholder]').trigger("blur");
}, 1);
This is really good and I've seen a lot of other improvements but I have one thing that one of my clients showed me that I thought was interesting. In IE8 with this plug and play code when you click in the input field the value or are make shift "Placeholder" in this case disappears. This behavior is not consistent with actual placeholders.
Actual placeholders are still visible when the user clicks into an input field. Only once the user starts typing does the placeholder go away. So I tried recreating this experience because my client said that is confusing. I think it is personally fine the way it is when you click in and the "Placeholder" goes away but the client is always right so I decided that I would make a small change to this snippet.
Instead of doing .focus
I used .keypress
and it seemed to work okay. Kind of weird but was wondering if anyone either suggested this solution or if someone had a better solution for this situation.
It seems to be problem with IE 8. So when i have placeholder text where I have a password field. It replaces with *** in value.
please suggest me a solution.
I can by the way confirm that this doesn't work as expected in MSIE 8, the placeholders don't show up on page load.
Just as an aside, this would NEVER have worked as expected, because the original code never loads the contents of the placeholder on page load.
I'll fork this with correct code, and link to that, rather than adding more confusion to this page.
Again, I would recommend against using the top code as is because it's incomplete and technically wrong, but the core logic is fine, just incomplete and inadequate to most standard use cases. It looks like it wasn't actually tested on the browsers it was targeted at since the issues would have been obvious immediately.
This is imperfect way. please refer https://github.com/mathiasbynens/jquery-placeholder.
Hi. I'm searching for a fix to force placeholders in IE. Came across this one and it seems pretty simple, but IE is still not displaying my placeholders. I'm sure I've missed something obvious, admittedly I'm not very good with jQuery.
You can view the site here: http://www.stellarcreativeservices.com
Thanks in advance for your help!!