Skip to content
Advertisement

how to change multiple input value with onchange jquery

first i have input with name=”ot_jam[‘+ number +’]” and onchange”otNormal(this.value)”, second i have input with name=”ot_uang[‘+ number +’]” so now i have multiple input and i want to every ot_jam just change value ot_uang with same name, my problem is im stuck in when i have multiple input and i change value ot_jam but i just change the last input with name ot_uang

sorry my english so bad im still learning.

This my HTML Code

JavaScript

This my Js Code

JavaScript

Advertisement

Answer

The reason you were only getting the last input was due to “var number” inside otNormal(). It was always going to get the last number by using the length. You can grab the correct number by taking it from the otjam element name instead.

Get correct number Example:

const number = e.target.name.replace(/[^0-9]/g, '');

I refactored your code a little bit to make it easier to edit later and added step by step comments to help break down what each line is doing.

Document.ready()

  • Cached the elements you will be using. (barisDiv & tableElement)
  • Changed click to on.click, also moved it out of the html
  • Added change event to the parent table instead of the inputs
  • Moved button click callback out of html

otNormal()

  • changed function to return changed value (change event will update uAng now)

BarisBaru()

  • removed document.ready() (Not needed here)

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement