1

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>

2 Answers 2

1

$('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()

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

2 Comments

thanks but what if I want to get the key only without the whole name for example 0 instead of item_qty[0]
use trhis $(this).index()
0

The change event only fires once the text-input is blurred (the user tabs, or clicks, out of that <input>); if that's what you want to happen then:

$('input[type="text"]').on('change', function(e) {
  // e: is a reference to the event.

  var index = $(this).index(),
      name = this.name;

  console.log("Name: " + name + ", index: " + index);
});
<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]" />

Or, in plain JavaScript it's still relatively easy (albeit somewhat more verbose):

// a simple utility function to retrieve the element's index:
function getIndex(node) {

  // assigning the node to the 'current' variable:
  var current = node,

    // initialising a 'count' variable at 0:
    count = 0;

  // while there is a current node, and that current node
  // has a previousElementSibling (a previous sibling that
  // is an HTMLElement):
  while (current && current.previousElementSibling) {

    // increment the count:
    count++

    // assign the previous HTMLElement sibling to
    // the current variable and repeat:
    current = current.previousElementSibling;
  }

  // return the final value of the count:
  return count;
}


// a simple named function to retrieve the relevant details:
function getDetails() {

  // caching the 'this' into the 'node' variable:
  var node = this;

  // logging the details (you could also return here):
  console.log({

    // the name of the element node:
    'name': node.name,

    // the index of the element node:
    'index': getIndex(node),

    // and the value of the element node
    // (to show how to access existing
    // other properties of the node,
    // although that wasn't specified in
    // the question):
    'value': node.value
  });
}


// creating an Array from the Array-like HTMLCollection returned
// by document.querySelectorAll(), and iterating over that Array
// using Array.prototype.forEach():
Array.from(document.querySelectorAll('input[type="text"]')).forEach(function(input) {
  // 'input', the first argument, is a reference to the
  // current Array-element of the Array over which
  // we're iterating.

  // here we assign the named function (note the lack of
  // parentheses) as the 'change' event handler:
  input.addEventListener('change', getDetails);
});
input {
  display: block;
  margin: 0 0 0.4em 0.4em;
}
<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]" />

References

Comments

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.