2
 <form id="form1" runat="server">

  <table border="1" id="ResultSheet">
      <tr  id ="Name">
          <td> Student Name</td>
          <td><input type="text" /></td>
      </tr>
      <tr>
          <td>Subject Name</td>
      </tr>
      <tr>
          <td>Maths</td>
          <td><input type="text" /></td>
      </tr>
      <tr>
          <td>Science</td>
          <td><input type ="text" /></td>
      </tr>
      <tr>
          <td>English</td>
          <td><input type ="text" /></td>
      </tr>
   </table>
 </form>

I have this table and now I want to get the value of each of input type. how do I get it.

I've tried:

document.getElementById("ResultSheet").rows[2].cells.item(0).innerHTML);

But it did not return anything.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
Vicky
  • 31
  • 4

2 Answers2

0

You could use querySelectorAll() like :

document.querySelectorAll('#ResultSheet input');

The loop through the result like the snippet below shown.

Hope this helps.

var inputs = document.querySelectorAll('#ResultSheet input');

for(var i=0;i<inputs.length;i++){
   console.log(inputs[i].value);
}
<form id="form1" runat="server">
  <table border="1" id="ResultSheet">
      <tr  id ="Name">
          <td> Student Name</td>
          <td><input type="text" value="1"/></td>
      </tr>
      <tr>
          <td>Subject Name</td>
      </tr>
      <tr>
          <td>Maths</td>
          <td><input type="text"  value="2"/></td>
      </tr>
      <tr>
          <td>Science</td>
          <td><input type ="text"  value="3"/></td>
      </tr>
      <tr>
          <td>English</td>
          <td><input type ="text"  value="4"/></td>
      </tr>
   </table>
 </form>
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
0

Use <form> property elements: https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/elements

Example: https://jsfiddle.net/8co9v0ye/

Cezary Tomczyk
  • 584
  • 1
  • 7
  • 14