40

I have a

<input type="text" value="A new value">

I need a javascript method to clear the value of the textbox when the focus is on the textbox.

How can this be achieved?

kalls
  • 2,797
  • 17
  • 55
  • 75

15 Answers15

49

just get element using

   function name()
   {  
   document.getElementById('elementid').value = "";
   }

you can call this function on onfocus event of textbox and clear the value

zish
  • 1,112
  • 1
  • 10
  • 17
24

It sounds like you're trying to use a "watermark" (a default value that clears itself when the user focuses on the box). Make sure to check the value before clearing it, otherwise you might remove something they have typed in! Try this:

<input type="text" value="A new value" onfocus="if(this.value=='A new value') this.value='';">

That will ensure it only clears when the value is "A new value".

Colin O'Dell
  • 8,386
  • 8
  • 38
  • 75
  • This solves the problem. however, I have a cancel button which should set the text bac to "A New value". Another javascript method? – kalls Nov 09 '10 at 16:23
  • 2
    @Kalls: If that cancel button is a normal ``, then it should do that. – RoToRa Nov 09 '10 at 16:27
  • RoToRa is correct, a "reset" button should do the trick. If you're using a custom JS function to "cancel", add this code to it: `document.getElementById('IDofthetextbox').value = "A new value";` – Colin O'Dell Nov 09 '10 at 16:57
  • I have a Jquery method on the cancel button which hides a DIV. How can I write code to identify and set the text of the textbox? – kalls Nov 09 '10 at 17:40
  • 2
    @Kalls: You'll need to give the input an ID and put this inside that method: `$('#mytextbox').val('');` where 'mytextbox' is the ID. – Colin O'Dell Nov 09 '10 at 19:24
  • 1
    It would make more sense to use the `placeholder` attribute and avoid the need to use JavaScript. – Dan Bray Jul 10 '17 at 18:22
11

For those coming across this nowadays, this is what the placeholder attribute was made to do. No JS necessary:

<input type="text" placeholder="A new value">
G Rice
  • 156
  • 1
  • 4
10
<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';}" />
KeatsKelleher
  • 10,015
  • 4
  • 45
  • 52
7

just set empty string

    <input type="text" id="textId" value="A new value">
    document.getElementById('textId').value = '';
SwapnilP
  • 137
  • 2
  • 7
6

Simple one

onfocus="this.value=''" onblur="if(this.value==''){this.value='Search...';}"
Sashi
  • 686
  • 8
  • 21
6

use sth like

<input type="text" name="yourName" placeholder="A new value" />

asdf
  • 460
  • 1
  • 10
  • 31
5

If using jQuery is acceptable:

jQuery("#myTextBox").focus( function(){ 
    $(this).val(""); 
} );
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
3

Give this in input tag of textbox:

onClick="(this.value='')" 
animuson
  • 53,861
  • 28
  • 137
  • 147
Lucky13
  • 11,393
  • 7
  • 25
  • 36
2
<input type="text" value="A new value" onfocus="this.value='';">

However this will be very irrigating for users that focus the element a second time e.g. to correct something.

RoToRa
  • 37,635
  • 12
  • 69
  • 105
2

Use the onfocus and onblur events like this:

<input type="text" name="yourName" value="A new value" onfocus="if (this.value == 'A new value') this.value = '';" onblur="if (this.value == '') this.value = 'A new value';">
AlexV
  • 22,658
  • 18
  • 85
  • 122
1
<input type="text" name="yourName" value="A new value" onfocus="if (this.value == 'A new value') this.value =='';" onblur="if (this.value=='') alert('Please enter a value');" />
1

Onfous And onblur Text box with javascript

   <input type="text" value="A new value" onfocus="if(this.value=='A new value') this.value='';" onblur="if(this.value=='') this.value='A new value';"/>
Dishan TD
  • 8,528
  • 7
  • 26
  • 41
1

Your HTML code,

jquery code,

$("#textboxID").val('');

bunny
  • 19
  • 3
0

For my coffeescript peeps!

#disable Delete button until reason is entered
$("#delete_event_button").prop("disabled", true)
$('#event_reason_is_deleted').click ->
    $('#event_reason_is_deleted').val('')
    $("#delete_event_button").prop("disabled", false)
Mark Locklear
  • 5,044
  • 1
  • 51
  • 81