Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the row data using jquery by clicking buttons for that row

I am having issues to get the table data in the row if a button is selected. I have two buttons approve and deny and based on what button user clicks I want to grab the data using query. can get the row numbers and stuff just not the row data. I need to get the id and tester.

here is what I have

<table id="mytable" width="100%">
<thead>
<tr>
<th>ID</th>
<th>Tester</th>
<th>Date</th>
<th>Approve</th>
<th>Deny</th>
</tr>
</thead>
<tbody>
<tr class="test">
<td class="ids">11565 </td>
<td class="tester">james</td>
<td>2012-07-02 </td>
<td><Button id="Approved" type="submit" >Approved</button>
</td>
<td><Button id="deny_0" type="submit" >Denied</button>
</td>
</tr>
</tbody>
</table>

here is my javascript to get the tr and td number but I am not sure how to use it to get what I need

$(document).ready(function() {  

    /*$('#cardsData .giftcardaccount_id').each(function(){

        alert($(this).html());
     }); */
    $('td').click(function(){
          var col = $(this).parent().children().index($(this));
          var row = $(this).parent().parent().children().index($(this).parent());
          alert('Row: ' + row + ', Column: ' + col);
         // alert($tds.eq(0).text());
          console.log($("tr:eq(1)"));
         // $("td:eq(0)", this).text(),

        });


});
like image 250
Asim Zaidi Avatar asked Jul 02 '12 23:07

Asim Zaidi


People also ask

How can get number of rows of table in jQuery?

To count the number of rows, the “#Table_Id tr” selector is used. It selects all the <tr> elements in the table. This includes the row that contains the heading of the table. The length property is used on the selected elements to get the number of rows.

How can get TD table row value in jQuery?

$('#mytable tr'). each(function() { var customerId = $(this). find("td:first"). html(); });


2 Answers

$(document).ready(function(){
    $('#Approved').click(function(){
        var id = $(this).parent().siblings('.ids').text();
        var tester = $(this).parent().siblings('.tester').text();

        console.log(id);
        console.log(tester);
    });
});​

JSFiddle

like image 68
xbonez Avatar answered Oct 30 '22 18:10

xbonez


$(function(){
    $('button').on('click', function(){
        var tr = $(this).closest('tr');
        var id = tr.find('.ids').text();
        var tester = tr.find('.tester').text();
        alert('id: '+id+', tester: ' + tester);
    });
});​

FIDDLE

like image 27
adeneo Avatar answered Oct 30 '22 17:10

adeneo