I have a
I need a javascript method to clear the value of the textbox when the focus is
just set empty string
<input type="text" id="textId" value="A new value">
document.getElementById('textId').value = '';
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".
<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';}" />
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)
<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');" />
<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.