Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear text field on focus of text field

I want to clear the text field when the user clicks on that

<input name="name" type="text" id="input1" size="30" maxlength="1000" value="Enter Postcode or Area" onfocus=="this.value=''" />

like image 430
Gaurav Srivastava Avatar asked Mar 16 '11 10:03

Gaurav Srivastava


2 Answers

Unless you are doing something specific where you only want to clear onclick, I would suggest (as others have noted) to use the onfocus actions instead. This way if someone is using tab to navigate it will also clear the default text.

You can also use onblur to check if it's empty to bring it back:

 <input type="text" value="Default text" name="yourName" onfocus="if(this.value == 'Default text') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Default text'; }">
like image 63
ORyan Avatar answered Sep 21 '22 08:09

ORyan


Or you can simply use the placeholder attribute For example<input name="name" type="text" id="input1" size="30" maxlength="1000" placeholder="Enter Postcode or Area"/>

like image 41
Vishal Kumar Avatar answered Sep 21 '22 08:09

Vishal Kumar