Wednesday, March 31, 2010

Add a text to text box, hide when it is clicked, show when clicked out

My Friend Ukesh gave me this code, thanks to him for saving my time.

<input name="q" id="search_text" type="text" value="- Search -" onfocus="clearDefaultText(this)" onblur="clearDefaultText(this)" style="background:none"/>
<script>
function clearDefaultText(field){

if (field.defaultValue == field.value) field.value = '';
else if (field.value == '') field.value = field.defaultValue;

}
</script>

2 comments:

Unknown said...

common dude, its just a Javascript :)

Srikanth Jeeva said...

Yeah it takes 15-20 mins when I do it, but it took seconds to copy :):) Is there any option in the input tag for doing this kinda work??