El Hamza El Hamza - 13 days ago 4
HTML Question

Get value of class with dynamic id jquery

I have the table bellow and I want to get the input

$('.note')
value from the
id
of the previous input :

<tr>
<td><input type="text" id='student_1' class='student'></td>
<td><input type="text" class='note'></td>
</tr>
<tr>
<td><input type="text" id='student_2' class='student'></td>
<td><input type="text" class='note'></td>
</tr>


So it can be something like that :

$(".student").change(function () {
alert(this.id.parent('td input.note').val())
})


Please someone help me.
Thanks.

Answer

You can use a combination of closest and find.

$(".student").on('change', function () {
   // the student input to which the change event is bound
    var $this = $(this);
    // Get the wrapper in which the inputs are present
    var $closestTr = $this.closest('tr');
    // the input vale that is needed
    alert($closestTr.find('.note').val());

});
Comments