El Hamza El Hamza - 1 year ago 56
HTML Question

Get value of class with dynamic id jquery

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

value from the
of the previous input :

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

So it can be something like that :

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

Please someone help me.

Answer Source

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