Vikram Anand Bhushan
Vikram Anand Bhushan

Reputation: 4896

How to traverse all the column of a current row in a table using Jquery

I have a row in a table something like this

<table>
    <tr>
        <td>Corredor Feed</td>
        <td>Id Corrdor
            <input type="text" value="" class="validate" name="idcorreo">
        </td>
        <td>Nombre
            <input type="text" value="" class="validate" name="nombre">
        </td>
        <td>Email
            <input type="text" value="" class="validate" name="email">
        </td>
        <td>Empressa
            <input type="text" value="" class="validate" name="Empressa">
        </td>
        <td>Pagina Web
            <input type="text" value="" class="validate" name="paginaWeb">
        </td>
        <td>Telefono
            <input type="text" value="" class="validate" name="telephon">
        </td>
        <td>Cellular
            <input type="text" value="" class="validate" name="cellular" />
        </td>
        <td>

            <input type="button" id="guardarBtn" value="Save" name="submitme">
        </td>
    </tr>
</table>

I am trying to access the value of column number 3 that is email when i click on the Save button.

My script is something like this

$(document).on('click', '#guardarBtn', function (event) {
    alert('yo');
    var tr = $(this).parents('tr');
    tr.each('td', function () {
        alert($(this).html()) ;

    });
    // $(".validate", this).css("border", "1px solid #ddd");

});

Js Fiddle for the question is HERE

Thanks in Advance

Upvotes: 1

Views: 327

Answers (3)

Kartikeya Khosla
Kartikeya Khosla

Reputation: 18883

You can use eq() and .find() as shown below :-

$(document).on('click', '#guardarBtn', function (event) {
    var tr = $(this).parents('tr:first');
    alert(tr.find('td:eq(3) input:text').val());
});

Upvotes: 1

Lexi
Lexi

Reputation: 1730

You can also go directly to named inputs with jQuery via attribute searches. For instance

var email = $('input[name="email"]').val();

Upvotes: -1

Rory McCrossan
Rory McCrossan

Reputation: 337691

You can select the td by index using :eq():

$(document).on('click', '#guardarBtn', function (event) {
    var $tr = $(this).closest('tr');
    var email = $tr.find('td:eq(3) input').val();
    alert(email);
});

Example fiddle

Upvotes: 2

Related Questions