I wanted to extract the array index of the selected input text from data table row
(function($) {
$('table tbody').on('click', '.btn', function(){
var tr = $(this).closest('tr');
var a = $(tr).find('input.dataform').attr('name');
alert(a);
});
})
<table>
<tbody>
<tr>
<td><input type="text" name="dataform[1][firstname]" /></td>
<td><button type="button" >Go</button></td>
</tr>
<tr>
<td><input type="text" name="dataform[2][firstname]" /></td>
<td><button type="button" >Go</button></td>
</tr>
</tbody>
</table>
Suppose button in first row fired; var a return dataform[1][firstname] and that is correct. However I lso wanted to extract the input name array index which is 1
CodePudding user response:
The code bellow work, or you can try add a data-idx="1" on button and you add also a data-idx="1" on closest input, so you can get fast with $('input[data-idx="' $(this).attr('data-idx') '"]').attr('name');
$(function() {
$('.btn').on('click', function(){
var tr = $(this).parent().parent().find('tr');
$(tr).hide();
var a = $(this).parent().parent().find('input.dataform').attr('name');
alert(a);
});
});
<table>
<tbody>
<tr>
<td><input type="text" name="dataform[1][firstname]" /></td>
<td><button type="button" >Go</button></td>
</tr>
<tr>
<td><input type="text" name="dataform[2][firstname]" /></td>
<td><button type="button" >Go</button></td>
</tr>
</tbody>
</table>
