Table Selector with Id

Asked

Viewed 31 times

0

Good morning, I have an excerpt of Javascript code that I am adding a row in the table, but when I implement in the system there is more than one table, I need to specify which one is with the id but I find it difficult to put with the code.

$("table tbody tr:last-child").index();

that’s what I add, I’d like to know how to put the id.

thank you

1 answer

-1


See this example of how the table ID is passed. It’s simple but it’s functional.

<table id="tabela">
    <tr>
        <td><input type="checkbox" class="opcao"></td>
        <td><input type="checkbox" class="opcao"></td>
        <td><input type="checkbox" class="opcao"></td>
    </tr>

</table>


    $(document).ready(function() {

    $('#check').click(function(event) {
        event.preventDefault();
        var valido = true;

        $("#tabela tr").each(function(index, el) {
            var $linha = $(el);
            var checked = $linha.find('.opcao:checked').length;
            if (checked == 0 ) {
                valido = false;
                return false;
            }
        });
        console.log('valido ' , valido);
        alert(valido ? 'checado' : 'erro');
    });
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.