Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: find all "input" in a table

Is there a shorter way to write this in JavaScript?

    var data = [];
    var table = document.getElementById( 'address' );
    var rows = table.getElementsByTagName( 'tr' );
    for ( var x = 0; x < rows.length; x++ ) {
        var td = rows[x].getElementsByTagName( 'td' );
        for ( var y = 0; y < td.length; y++ ) {
            var input = td[y].getElementsByTagName( 'input' );
            for ( var z = 0; z < input.length; z++ ) {
                data.push( input[z].id );
            }
        }
    }
like image 532
sid_com Avatar asked Sep 10 '11 12:09

sid_com


1 Answers

element.getElementsByTagName finds all descendants, not only children, so:

<script type="text/javascript> 

    var data = []; 
    var table = document.getElementById( 'address' ); 
    var input = table.getElementsByTagName( 'input' ); 
    for ( var z = 0; z < input.length; z++ ) { 
        data.push( input[z].id ); 
    } 

</script> 
like image 119
Jon Avatar answered Sep 17 '22 23:09

Jon