programer8
programer8

Reputation: 577

reading the value in an input tag in a html table using java script

<table id="myTable" style="border: 1px solid; border-color: black; border-collapse: collapse;">
    <thead  style="background-color: silver;">
        <tr>
            <th >1</th>
            <th >2</th>
            <th >3</th>
            <th >4</th>
        </tr>
    </thead>
    <tbody  id="my_form">
        <tr>
            <td > <input type="text"> </td>
            <td > <input type="text"></td>
            <td ><input type="text"> </td>
            <td ><input  type="text"> </td>
        </tr>               
    </tbody>
</table>

this is my html table and this is my js function to read it's contents

var mytable=document.getElementById('myTable');
var row_length=myTable.rows.length;
for(var i=1;i<row_length;i++)
    {
        var cells=change_key_table.rows.item(i).cells;
        var cell_length=cells.length;
        var content = cells.item(0).innerText;
            alert(content);
    }

this gives a blank string. innerText should not be used to read the value from the table? is there any specific method to read value from input tags? i read a lot of question in stack overflow regarding this but could find anything to solve the problem

Upvotes: 0

Views: 1927

Answers (1)

Quentin
Quentin

Reputation: 943569

innerText should not be used to read the value from the table?

It can't be used to read the value from an input.

is there any specific method to read value from input tags?

Method? No. You need to access the value property of the input (which will be a string).

You can bypass dealing with the table structure entirely as it doesn't seem to be doing anything significant.

var mytable = document.getElementById('myTable');
var myinputs = mytable.getElementsByTagName('input');
for (var i = 0; i < myinputs.length; i++) {
    alert(myinputs[i].value);
}

Upvotes: 2

Related Questions