Page 1 of 1

getElementById help

Posted: Thu Nov 24, 2005 2:33 pm
by taha
if i have the following line of code, could i somehow get the value of each textfield by assigning it to an array???

Code: Select all

function submitSave()
  {
     var iarray  = new Array();

     for (i=0; i<numItems; i++)
     {
         document.getElementById("textfields").value = iarray;         
     }
  }

Posted: Thu Nov 24, 2005 6:39 pm
by Chris Corbyn
This is JavaScript not PHP. Moved to client side ;)

Posted: Thu Nov 24, 2005 6:45 pm
by Chris Corbyn
You're assigning the array to the value, rather than adding the value to the array ;)

Code: Select all

function submitSave()
  {
     var iarray  = new Array();

     for (i=0; i<numItems; i++)
     {
         iarray.push(document.getElementById("textfields").value);
     }
  }
This is obviously pseudo code you posted but I'm not sure what you're after. If you want the value of all text fields this works.... (hint: document.getElementsByTagName('foo'); )

Code: Select all

function submitSave()
  {
     var iarray  = new Array();
     var textFields = document.getElementsByTagName('textarea'); //Returns array of nodes
     for (var x in textFields)
     {
         iarray.push(textFields[x].value);
     }
     return iarray;
  }