Clear text field/text area

RalphTrent
RalphTrent used Ask the Experts™
on
Hello
I am trying to create a function that will clear the contents of a text area.  I have this set up:
function clearDoc()
{
     document.test.text1.value="";
     document.test.text1.focus();
}

test = form name
text1 = name of text area.
This works, but I was wondering if anyone else knows an official way to clear the contents.  I have tried,
document.test.text1.clear(); but that did not work.
Thanks
Ralph
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
you could always create a clear() method yourself like this:

function clear()
{
    this.value = "";
    this.focus();
}

<BODY onload='document.test.text1.clear=clear;'>

then, after the page loads, you can call clear just as you suggested above:

<INPUT type='button' value='Clear' onclick='document.test.text1.clear();'>



(from one Ralph to another:)
a working example:


<HTML>
<HEAD>
<SCRIPT language='javascript'>
 function clear()
 {
    this.value = "";
    this.focus();
 }
</script>
</head>

<BODY onload='document.test.text1.clear=clear;'>

<FORM name='test' onsubmit='return false;'>
 <TEXTAREA name='text1'></textarea>
 <INPUT type='button' value='Clear' onclick='document.test.text1.clear();'>
</form>

</body>
</html>


Commented:
In javascript there is only a away to reset the whole form.

document.test.reset();

but that will also reset the other fields in the form! Otherwise just do as you've done in your own code, or KeK's.

CJ
thanks, but why only a B?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial