0

I have a html structure likes this:

<table>
   <tr>
     <td>
        <input class="text-center inputForSum" type="text" name="menge" value="1.00" />
     </td>
   </tr>
   <tr>
     <td>
        <input class="text-center inputForSum" type="text" name="menge" value="1.00" />
     </td>
   </tr>
   <tr>
     <td>
        <input class="text-center inputForSum" type="text" name="menge" value="1.00" />
     </td>
   </tr>
</table>

and a on change function:

$( ".inputForSum" ).change(function() {
   console.log(  $('.inputForSum').val()  )
});

But I need to know, WHICH input field with the class "inputForm" has changed. How can I realize this?

2 Answers 2

1

Is the index of the tr parent an acceptable answer ?

$( ".inputForSum" ).change(function(ev) {
   console.log($(ev.target).closest("tr").index(), $(ev.target).val())
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
   <tr>
     <td>
        <input class="text-center inputForSum" type="text" name="menge" value="1.00" />
     </td>
   </tr>
   <tr>
     <td>
        <input class="text-center inputForSum" type="text" name="menge" value="1.00" />
     </td>
   </tr>
   <tr>
     <td>
        <input class="text-center inputForSum" type="text" name="menge" value="1.00" />
     </td>
   </tr>
</table>

Sign up to request clarification or add additional context in comments.

Comments

1

$( ".inputForSum" ).change(function() {
   console.log(  $(this).val()  )
   console.log($(this).closest('tr'))
});
<script
  src="https://code.jquery.com/jquery-3.6.0.min.js"
  integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
  crossorigin="anonymous"></script>
<table>
   <tr>
     <td>
        <input class="text-center inputForSum" type="text" name="menge" value="1.00" />
     </td>
   </tr>
   <tr>
     <td>
        <input class="text-center inputForSum" type="text" name="menge" value="1.00" />
     </td>
   </tr>
   <tr>
     <td>
        <input class="text-center inputForSum" type="text" name="menge" value="1.00" />
     </td>
   </tr>
</table>

You can use $(this) to get the input that changed

it s $(this).val()

2 Comments

okay thank you ! but my explanation was wrong. I need to now, which <tr> element is the parent of the changed inputForSum value. because in this tr tags are other input values, which I need to change with my on change function
u can use $(this).closest('tr')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.