/ Published in: jQuery
When you click on the text box the default value (such as "write here") disappears and you can type in. On blur if the textbox is empty it will appear the default value again.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<style> input.csson { color: red; } input.cssoff { color: gray; } </style> <form> <input id='author' type='text' value='' rel="writehere|type author|cssoff|csson"/><br/> <input id='topic' type='text' value='' rel="writehere|type topic|cssoff|csson"/><br/> </form> function setWriteHere(search) { /* add on click event handler */ $("input[rel^="+search+"]").click( function () { var ar = $(this).attr('rel').split('|'); if($(this).val()==ar[1]) { $(this).val(""); $(this).attr('class',ar[3]); } } ); /* add on blur event handler */ $("input[rel^="+search+"]").blur( function () { var ar = $(this).attr('rel').split('|'); if($(this).val()=="") { $(this).val(ar[1]); $(this).attr('class',ar[2]); } } ); /* trigger blur event */ $("input[rel^="+search+"]").each( function () { $(this).blur(); } ); } $(document).ready(function() { /* fix forms when ready */ setWriteHere("writehere"); } );
URL: http://www.barattalo.it/2009/11/17/set-write-here-on-input/