1

How to get value in javascript when input name/id generated by looping, just like this.

name="items[1AV08EBfeb][srdnReqQty1AV08EBfeb]"

--js--
var form = document.forms[0];
var get = form.items[1AV08BBjan][srdnReqQty1AV08BBjan].value;

alert(get); //error missing ]

--js--

Any suggestion ?

Thanks
MRizq

3
  • Always use id attribute for html elements Commented Jul 29, 2011 at 7:52
  • @Shakti: No, you kind of need name if you're going to submit the form element. id and name serve different purposes, and each has its use. Commented Jul 29, 2011 at 7:55
  • ::naveen: i want to get their value and make some condition. ::Shakti Singh: id/name returned same value, id="items[1AV08EBfeb][srdnReqQty1AV08EBfeb]" Commented Jul 29, 2011 at 7:58

1 Answer 1

3

If the name of the element really is

name="items[1AV08EBfeb][srdnReqQty1AV08EBfeb]"

...as you've shown, then:

var form = document.forms[0];
var get = form.elements["items[1AV08EBfeb][srdnReqQty1AV08EBfeb]"].value;

Live example

There's nothing special about the [ and ] characters within the name attribute of a form element at the HTML and JavaScript level. Some server-side technologies (like PHP) will look for those characters in the names of submitted form fields and turn them into arrays, but that's not an HTML or JavaScript thing.

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

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.