views:

42

answers:

4

what is the best method to clear text in a text field, when the user clicks on it...

i.e. if the text search field says "search" and when they click it, it clears the value..

thanks

+2  A: 

You could do like:

<input type="text" onfocus="if(this.value == 'search') {this.value=''}" onblur="if(this.value == ''){this.value ='search'}">
Sarfraz
A: 

In JavaScript you can simple set the value of the contorol to an empty string in the OnFocus event handler.

Oded
A: 

Normal HTML:

 <script type="text/javascript">
    function clearThis(target){
        target.value= "";
    }
    </script>
    <input type="text" value="Search" onfocus="clearThis(this)" />

jQuery:

<script type="text/javascript">
    function clearThis(target){
        $(target).val = "";
    }
</script>
<input type="text" value="Search" onfocus="clearThis(this)" />
Jason
A: 

If jquery's an option, I'd consider the watermark plugin

It achieves what you're after, but with a bit more style

AlexCuse