loop through text object values in DOM?

Discussion in 'Javascript' started by Tom Fitzgibbon, Dec 4, 2003.

  1. 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
    Tom Fitzgibbon, Dec 4, 2003
    #1
    1. Advertising

  2. Tom Fitzgibbon

    DB McGee Guest

    "Tom Fitzgibbon" <> wrote in message
    news:bqo59d$s8a$...
    > 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>
    DB McGee, Dec 4, 2003
    #2
    1. Advertising

  3. Thanks - works great
    -Tom

    "DB McGee" <> wrote in message
    news:F3Nzb.146811$...
    > "Tom Fitzgibbon" <> wrote in message
    > news:bqo59d$s8a$...
    > > 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>
    >
    >
    Tom Fitzgibbon, Dec 4, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tony collier
    Replies:
    2
    Views:
    358
    tony collier
    Jan 12, 2004
  2. Replies:
    0
    Views:
    538
  3. JR
    Replies:
    6
    Views:
    1,041
  4. Roedy Green
    Replies:
    3
    Views:
    415
    Mike Schilling
    Sep 13, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    352
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page