How it is possible that during onfocus (on input field) input default value seen like it is disabled but on input field could be written anything like default value isn’t exists ?
here is simple html =>
<input type="text" id="x">
and javascript =>
document.getElementById("x").onfocus = function(){
this.style.opacity = 0.5;
}
but I couldn’t do what I want.
With “HTML5”, new functionality and attributes have been introduced for form-Elements (such as built-in form-validation for example)
One of those is the
placeholder– attribute. It shows a specified text on empty input-Fields that will be hidden, after a user starts to fill text in the field.The HTML-Markup looks like this:
This feature is not supported by all browsers yet (you can check compatibility on caniuse.com
In your code, you can check for compatibility with a simple function:
For older browsers, you would need to write a fallback JavaScript – Function, that reads this attribute and implement the behaviour yourselve. There are some blog-posts about this around the web, like one from David Walsh, that could help you with this.
edit:
I stumbled over this gist by Hagenburger (according blog-post), that should implement the behaviour you want to achieve for old browsers too. Note: it’s jQuery – code, not sure if you are using it, but even if not, it should give you an idea, what to do.
So, given the compatibility – check from above:
Like that, the native placeholder-implementation of the browser would be used, if it exists, otherwise, the JavaScript-function would take care of this.
update 09/11/2012
SO-User and Moderator ThiefMaster just pointed out a better and newer jQuery-plugin by Mathias Bynens, that already has built-in check for
placeholder– support. Surely a better way to implement the placeholder-fallback than the gist I posted:jQuery Placeholder at github