javascript - Jquery - Get previous value row in table -
i have dynamic simple table like:
i try previous value cell when click edit button.
example: when click first edit button alert('a1')
when click second edit button alert('a2')
i try
$('.edit').click(function(){ alert($(this).parents('tr').prev().children().eq(1).text()); });
it's working first edit button because previous row has 1 row.
, it't not working second edit button.
how can (by dynamic previous row) http://jsfiddle.net/bwjbj/
ps: i'm working next row
alert($(this).parents('tr').nextall(':eq(' + ($(this).parent().siblings().eq(0).attr("rowspan")-1) + ')').children().eq(1).text());
http://jsfiddle.net/mblase75/xgdkd/
the problem second edit button, previous table row isn't row want -- want row 2 more before that, because that's rowspans begin.
or, general: want the table row belonging previous edit button. in case of first edit button, though, want previous row.
so, in code:
$('.edit').click(function () { var idx = $('.edit').index(this); // edit button this? if (idx > 0) { // first button var $tr = $('.edit').eq(idx-1).closest('tr'); // previous row } else { // not first button var $tr = $(this).closest('tr').prev('tr'); // previous edit button's row } var $td = $tr.find('td:nth-child(2)'); // second <td> of row alert($td.text()); });
compact version of same code:
$('.edit').click(function () { var idx = $('.edit').index(this), $tr = (idx) ? $('.edit').eq(idx-1).closest('tr') : $(this).closest('tr').prev('tr'), $td = $tr.find('td:nth-child(2)'); alert($td.text()); });
Comments
Post a Comment