I'd like to use HTML5's placeholder attribute (You can see it in cation in the newsletter at Thought Results). But when I use older browsers, of course, they don't render anything. I can use JavaScript to imitate it, but then, I shouldn't use it and it's done the old way. How can I have both HTML5 placeholder attribute, and at the same time simulate it for older browsers?
3 Answers
You can detect if a browser supports the attribute:
http://diveintohtml5.info/detect.html#input-placeholder
function supports_input_placeholder() {
var i = document.createElement('input');
return 'placeholder' in i;
}
If it does, do nothing. If it doesn't, you can use JS to grab the placeholder value and then insert it into the field as you see fit (perhaps as default value) and then add the appropriate interactions to simulate the HTML5 placeholder behaviors.

- 39,848
- 49
- 150
- 213
-
1This worked. I just didn't understand the `return 'placeholder' in i;` part. What it technically does? What it means? – Saeed Neamati Jul 03 '11 at 05:46
-
2The `in` operator tests if an object (in this case a `HTMLInputElement` object) has a given property. Since browsers that implement the `placeholder` attribute also add the `placeholder` property to the `HTMLInputElement` interface, this is an effective test of support for `placeholder`. – Domenic Jul 03 '11 at 05:53
-
@Diogo your edit is an entirely new answer. Instead of editing this answer, please create a new one. – DA. Aug 31 '12 at 17:02
-
@Diogo crap. I guess not. Odd that the system doesn't save that as well. – DA. Aug 31 '12 at 17:32
@marcgg wrote a great JQuery placeholder plugin that basically replicates the placeholder functionality for those browsers that don't support it.
https://github.com/marcgg/Simple-Placeholder
I searched through a lot of placeholder plugins before settling on this one and it works great so far. Originally found it in response to this similar question:

- 1
- 1

- 12,668
- 9
- 45
- 63
I recommend using Modernizr to detect this feature.
if (Modernizr.input.placeholder) {
// your placeholder text should already be visible!
} else {
// no placeholder support :(
// fall back to a scripted solution
}
If you're not interested in using this library you could use the following the code.
function supports_input_placeholder() {
var i = document.createElement('input');
return 'placeholder' in i;
}
Regarding to fallback support, if you're using jQuery you could use Simple Placeholder, which had its origin here at StackOverflow.

- 1
- 1

- 21,637
- 26
- 100
- 138