loop through text object values in DOM?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Fitzgibbon

    loop through text object values in DOM?

    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.Create Event.test1.val ue 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


  • DB McGee

    #2
    Re: loop through text object values in DOM?

    "Tom Fitzgibbon" <tkfitz@hotmail .com> wrote in message
    news:bqo59d$s8a $1@reader2.pani x.com...[color=blue]
    > 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.Create Event.test1.val ue 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
    >
    >[/color]

    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.getEle mentsByTagName( 'input' ) // store collection of all
    <input/> elements
    for ( i = 0; i < oInputs.length; i++ ) { // loop through and find <input
    type="text"/>
    if ( oInputs[i].type == 'text' ) {
    oTextBoxes.push ( oInputs[i] ); // found one - store it in the oTextBoxes
    array
    }
    }
    msg = "Found " + oTextBoxes.leng th + " text boxes";
    for ( i = 0; i < oTextBoxes.leng th; i++ ) { // Loop through the stored
    textboxes and output the value
    msg += "\nTextbox #" + ( i + 1 ) + " value = " + oTextBoxes[i].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="storeT extboxes()"></p>
    </body>
    </html>


    Comment

    • Tom Fitzgibbon

      #3
      Re: loop through text object values in DOM?

      Thanks - works great
      -Tom

      "DB McGee" <noreply@norepl y.com> wrote in message
      news:F3Nzb.1468 11$Fv8.35494@tw ister01.bloor.i s.net.cable.rog ers.com...[color=blue]
      > "Tom Fitzgibbon" <tkfitz@hotmail .com> wrote in message
      > news:bqo59d$s8a $1@reader2.pani x.com...[color=green]
      > > Stupid question:
      > >
      > > How do I loop through many text boxes on a page and get values for each[/color][/color]
      box[color=blue][color=green]
      > > and put into an array?
      > >
      > > For example document.Create Event.test1.val ue will not take an array[/color][/color]
      value[color=blue][color=green]
      > > for the object name. Tried to figure out if associative arrays might[/color][/color]
      help[color=blue][color=green]
      > > but failed miserably. Obviously I can name the text boxes anything, but
      > > can't get the values out.
      > >
      > > Thanks for the help -
      > > Tom
      > >
      > >[/color]
      >
      > Tested on IE5+, Mozilla 1.4 - just call the storeTextBoxes( ) function[/color]
      where ever[color=blue]
      > 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.getEle mentsByTagName( 'input' ) // store collection[/color]
      of all[color=blue]
      > <input/> elements
      > for ( i = 0; i < oInputs.length; i++ ) { // loop through and find <input
      > type="text"/>
      > if ( oInputs[i].type == 'text' ) {
      > oTextBoxes.push ( oInputs[i] ); // found one - store it in the[/color]
      oTextBoxes[color=blue]
      > array
      > }
      > }
      > msg = "Found " + oTextBoxes.leng th + " text boxes";
      > for ( i = 0; i < oTextBoxes.leng th; i++ ) { // Loop through the stored
      > textboxes and output the value
      > msg += "\nTextbox #" + ( i + 1 ) + " value = " + oTextBoxes[i].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="storeT extboxes()"></p>
      > </body>
      > </html>
      >
      >[/color]


      Comment

      Working...