Michael M. Isaac Michael M. Isaac - 3 months ago 49
jQuery Question

get the value and the key from input array by jquery

I have code something like that

<input type="text" name="item_qty[0]">
<input type="text" name="item_qty[1]">
<input type="text" name="item_qty[2]">
<input type="text" name="item_qty[3]">


and I want to specify by JQuery which input from the array has been changed (0,1,2,3) and get its value.

I tried the below code but I couldn't complete it I need to get the array key ($i) and the input value .

<SCRIPT LANGUAGE="JavaScript">
$(document).ready(function() {
$('input[name=item_qty[$i]]').change(function(){


});
});
</SCRIPT>

Answer

$('input').on('input',function(i,v){
console.log("index of input is " + $(this).index())
console.log("name of input is " + $(this).attr('name'))


})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="item_qty[0]">
<input type="text" name="item_qty[1]">
<input type="text" name="item_qty[2]">
<input type="text" name="item_qty[3]">

You can use the index of the input by using .index()

Comments