views:

30

answers:

2

Hi, I have a table which has textboxes in its cells. All these textboxes are created and populated dynamically by using PHP. I have to iterate through the cells and get the values.

By using the following code, I am able to get the innerHTML of the cells.

var tblLang = document.getElementById("tbl_Languages");
var tblrows = tblLang.rows;
for(var i=1; i<tblrows.length; i++){
var tblcells = tblrows[i].cells;
alert(tblcells[0].innerHTML);

The output for the given code is

<input background-color="#FFFFFF" haslayout="-1" id="txtname_ENU" value=" English" type="text">

How could I get the value of the inner textbox? Please help me.

A: 

Did you mean:


var textBoxValue = document.getElementById("txtname_ENU").value;
alert(textBoxValue);
loentar
I want to retrieve the value, but I cannot do it by calling using the ID of the textbox.
Rajkumar
ahh. then:<pre> for(var i=0; i<tblrows.length; i++) { var tblcells = tblrows[i].cells; for(var j=0; j<tblcells.length; j++) { alert(tblcells[j].firstElementChild.value); } }</pre>
loentar
Thanks loentar.
Rajkumar
+1  A: 

You want to get the actual dom node from the table cell instead of the innerHTML (a string). This will allow you to call .value on that node and you're all good. SOmething like:

   tblcells[0].firstChild.value
   // or iterate through children
   var childLength = tblcells.childNodes.length
   for(var i=0;i<childLength;i++){
     alert(tblCells.childNodes[i].value);
   }

Also note that in your code when you're iterating for(var i=1; i<tblrows.length; i++){ you're checking the length of your tblrows array every time which is slow. You should check that length once, as in my code first, then use it in the loop.

brad
Hey Brad, Thanks a ton. The first one works fine.Between, are there any websites which serve as a document repository for Javascript??
Rajkumar
mozilla is always good:https://developer.mozilla.org/en/JavaScriptlook at the guide and core reference, also a good, short book is Douglas Crockfords "javascript, the good parts"
brad