Javascript Element How to - Find all input element inside a table








Question

We would like to know how to find all input element inside a table.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
window.onload=function(){<!--from   ww  w . j av  a  2 s.c  om-->
    var table, inputs, arr;
    table = document.getElementById( 'test' );
    inputs = table.querySelectorAll( 'input' );
    arr = [].slice.call( inputs ).map(function ( node ) {
        return node.id; 
    });
    console.log( arr );
}
</script>
</head>
<body>
  <table id="test">
    <tr>
      <td><input id="foo"></td>
      <td><input id="bar"></td>
      <td><input id="baz"></td>
    </tr>
  </table>
</body>
</html>

The code above is rendered as follows: