0

i have login control and textbox inside , i need to write UserName in textbox to show to user and i need when click inside textbox to clear this textbox and allow user to enter textenter image description here

Mounir
  • 281
  • 3
  • 10
  • 25
  • http://stackoverflow.com/questions/108207/how-do-i-make-an-html-text-box-show-a-hint-when-empty – Adam Aug 08 '12 at 08:53
  • This link has all you need: http://stackoverflow.com/questions/4135818/how-to-clear-a-textbox-using-javascript – Occidio Aug 08 '12 at 08:53

4 Answers4

1

use the placeholder html5 attribute

YardenST
  • 5,119
  • 2
  • 33
  • 54
1

Since you asked for javascript code

<input type="text" value="your name" name="usrname" onfocus="if(this.value==this.defaultValue) this.value='';" onblur="if(this.value=='') this.value=this.defaultValue;"/>
Sibu
  • 4,609
  • 2
  • 26
  • 38
1

if using jQuery is acceptable:

jQuery("#myTextBox").focus( function(){ 
    $(this).val(""); 
} );

or in javascript

<input type="text" value="A new value" onfocus="javascript: if(this.value == 'A new value'){ this.value = ''; }" onblur="javascript: if(this.value==''){this.value='A new value';}" />
Xyz
  • 5,955
  • 5
  • 40
  • 58
Nikhil D
  • 2,479
  • 3
  • 21
  • 41
0

Last year I wrote a jQuery plugin for exact that problem. It's called "toggletextfieldvalue" check it out http://www.naden.de/blog/toggle-textfield-value-jquery-plugin

Note: If you are using HTML5, you can rely on the new placeholder attribute.

naden
  • 126
  • 8