javascript – How to get values ​​from a table?

Question:

My question is very basic:

I have a table , I need to store in a variable all the values ​​of the row that was clicked, I'm trying this with this method below but the result is "undefined".

What could be wrong with the code? Or how could I do it?

 $(document).ready(function () {
    var table = $('#example2').DataTable();

    $('#example2 tbody').on('click', 'tr', function () {
        var data = table.row(this).data();
        alert(data[0]);
    });
});

  $(document).ready(function () {
    $('#example2').DataTable({
        "ajax": 'http://localhost:55959/Cliente/getBydesc?json=""',
        "columns": [
            { "data": "codigo" },
            { "data": "codinterno" },
            { "data": "razao" },
            { "data": "fantasia" },
            { "data": "cnpj" },
            { "data": "ie" },
            { "data": "cep" },
            { "data": "endereco" },
            { "data": "numero" },
            { "data": "complemente" },
            { "data": "bairro" },
            { "data": "cidade" },
            { "data": "uf" },
            { "data": "email" },
            { "data": "fone1" },
            { "data": "fone2" },
            { "data": "fone3" },
            { "data": "datacad" },
            { "data": "proprietario" }
        ],
        "columnDefs": [
             { "targets": [0, 2, 3, 18], "visible": true },
             { "targets": '_all', "visible": false },
             { "targets": [18], "width": "20%" },
             { "targets": [2, 3, 18], 'className': 'mdl-data-table__cell--non-numeric' },           
        ],
        "buttons": [
            'copy'
        ]
    });
});

html

 <table id="example2" class="display" cellspacing="0">
        <thead>
            <tr>
                <th>codigo</th>
                <th>codinterno</th>
                <th>razao</th>
                <th>fantasia</th>
                <th>cnpj</th>
                <th>ie</th>
                <th>cep</th>
                <th>endereco</th>
                <th>numero</th>
                <th>complemente</th>
                <th>bairro</th>
                <th>cidade</th>
                <th>uf</th>
                <th>email</th>
                <th>fone1</th>
                <th>fone2</th>
                <th>fone3</th>
                <th>datacad</th>
                <th>proprietario</th>
            </tr>
        </thead>          
    </table>

Answer:

You can use $("tr th").next(); and loop.

Scroll to Top