Skip to content

Instantly share code, notes, and snippets.

@3rd-Eden
Created March 25, 2010 10:56
Show Gist options
  • Save 3rd-Eden/343419 to your computer and use it in GitHub Desktop.
Save 3rd-Eden/343419 to your computer and use it in GitHub Desktop.
Detect if the placeholder attribute is support, if not find all elements and provide support for it through event listeners.
(function(){
if( function(){ var input = document.createElement( "input" ); return ( "placeholder" in input )}() )
return;
Spry.$$( "input[placeholder]", document.body ).forEach( function( e ){
var current = e.attributes.getNamedItem( "placeholder" ).value;
if( !e.value ){
e.value = current;
// add special place holder class for styling...
Spry.Utils.addClassName( e, "placeholder" );
}
Spry.Utils.addEventListener( e, "focus", function(){ this.value = this.value == current ? "" : this.value; return this.value == current ? Spry.Utils.addClassName( e, "placeholder" ) : Spry.Utils.removeClassName( e, "placeholder") });
Spry.Utils.addEventListener( e, "blur", function(){ this.value = this.value == "" ? current : this.value; return this.value == current ? Spry.Utils.addClassName( e, "placeholder" ) : Spry.Utils.removeClassName( e, "placeholder") });
})
}())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment