loop through text object values in DOM?

T

Tom Fitzgibbon

Stupid question:

How do I loop through many text boxes on a page and get values for each box
and put into an array?

For example document.CreateEvent.test1.value will not take an array value
for the object name. Tried to figure out if associative arrays might help
but failed miserably. Obviously I can name the text boxes anything, but
can't get the values out.

Thanks for the help -
Tom
 
D

DB McGee

Tom Fitzgibbon said:
Stupid question:

How do I loop through many text boxes on a page and get values for each box
and put into an array?

For example document.CreateEvent.test1.value will not take an array value
for the object name. Tried to figure out if associative arrays might help
but failed miserably. Obviously I can name the text boxes anything, but
can't get the values out.

Thanks for the help -
Tom

Tested on IE5+, Mozilla 1.4 - just call the storeTextBoxes() function where ever
you need it

<html>
<head>
<title>Count Textboxes</title>
<script type="text/javascript">
function storeTextboxes() {
oTextBoxes = new Array(); // to store the textbox objects
oInputs = document.getElementsByTagName( 'input' ) // store collection of all
<input/> elements
for ( i = 0; i < oInputs.length; i++ ) { // loop through and find <input
type="text"/>
if ( oInputs.type == 'text' ) {
oTextBoxes.push( oInputs ); // found one - store it in the oTextBoxes
array
}
}
msg = "Found " + oTextBoxes.length + " text boxes";
for ( i = 0; i < oTextBoxes.length; i++ ) { // Loop through the stored
textboxes and output the value
msg += "\nTextbox #" + ( i + 1 ) + " value = " + oTextBoxes.value
}
alert( msg );
}
</script>
</head>
<body>
<p><input type="text" name="ele1" size="30"></p>
<p><input type="text" name="ele2" size="30"></p>
<p><input type="text" name="ele3" size="30"></p>
<p><input type="text" name="ele4" size="30"></p>
<p><input type="text" name="ele5" size="30"></p>
<p><input type="button" value="go" onClick="storeTextboxes()"></p>
</body>
</html>
 
T

Tom Fitzgibbon

Thanks - works great
-Tom

DB McGee said:
Tom Fitzgibbon said:
Stupid question:

How do I loop through many text boxes on a page and get values for each box
and put into an array?

For example document.CreateEvent.test1.value will not take an array value
for the object name. Tried to figure out if associative arrays might help
but failed miserably. Obviously I can name the text boxes anything, but
can't get the values out.

Thanks for the help -
Tom

Tested on IE5+, Mozilla 1.4 - just call the storeTextBoxes() function where ever
you need it

<html>
<head>
<title>Count Textboxes</title>
<script type="text/javascript">
function storeTextboxes() {
oTextBoxes = new Array(); // to store the textbox objects
oInputs = document.getElementsByTagName( 'input' ) // store collection of all
<input/> elements
for ( i = 0; i < oInputs.length; i++ ) { // loop through and find <input
type="text"/>
if ( oInputs.type == 'text' ) {
oTextBoxes.push( oInputs ); // found one - store it in the oTextBoxes
array
}
}
msg = "Found " + oTextBoxes.length + " text boxes";
for ( i = 0; i < oTextBoxes.length; i++ ) { // Loop through the stored
textboxes and output the value
msg += "\nTextbox #" + ( i + 1 ) + " value = " + oTextBoxes.value
}
alert( msg );
}
</script>
</head>
<body>
<p><input type="text" name="ele1" size="30"></p>
<p><input type="text" name="ele2" size="30"></p>
<p><input type="text" name="ele3" size="30"></p>
<p><input type="text" name="ele4" size="30"></p>
<p><input type="text" name="ele5" size="30"></p>
<p><input type="button" value="go" onClick="storeTextboxes()"></p>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top