Help - Search - Members - Calendar
Full Version: Clear all textareas with a function
HTMLHelp Forums > Programming > Client-side Scripting
robby
I'm trying to make a function that when executed clears all textareas. Initially, this seemed like a really easy feat, but it didn't work.

This is what I had tried:

<script>
function clear(){
document.getElementsByTagName('textarea').value='';
}
</script>

<textarea>Test1</textarea>
<textarea>test2</textarea>

<input type="button" value="clear" onclick="clear()">

I'm not sure why it isn't working. Help is much appreciated. smile.gif
Christian J
CODE
document.getElementsByTagName('textarea')


returns an array of all textareas. To specify a particular one you'll need e.g.

CODE
document.getElementsByTagName('textarea')[0]


where "0" is its index number in the array (first one is zero). If there are many (or an unknown number of) textareas you probably want to create a FOR loop and clear each of them in it.
robby
QUOTE(Christian J @ Aug 16 2009, 08:17 PM) *

CODE
document.getElementsByTagName('textarea')


returns an array of all textareas. To specify a particular one you'll need e.g.

CODE
document.getElementsByTagName('textarea')[0]


where "0" is its index number in the array (first one is zero). If there are many (or an unknown number of) textareas you probably want to create a FOR loop and clear each of them in it.
Doh! I can't believe I forgot that. Many thanks! happy.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.