tags:

views:

24

answers:

3

I was not able to get part of this javascript code working for unknown reason and display as undefined. How do I merge vote[1] into the formObj which is document.forms[0] Any other alternate solution?

var elements2 = formObj.elements['vote[' + pollId + ']';
A: 

There is a basic syntax error:

var elements2 = formObj.elements['vote[' + pollId + ']';

Should be

var elements2 = formObj.elements['vote[' + pollId + ']'];
Kristoffer S Hansen
A: 

Could it be that you want:

var elements2 = formObj.vote[pollId];

(Assuming "vote" is the name of several form elements)

You might want to read about how to handle forms in JavaScript.

Felix Kling
A: 

I'm not a javascript programmer really, but from what I can see above in the code you are missing a "]" at the end of elements.

It looks like your setting elements2 to formObject.elements[i] where you using vote[pollId] as the index. So vote[pollId] should return an integer in this scenario.

I'm not sure if I understand the question

Javascript Arrays

var formObj = document.forms[0];
var i = formObj.length + 1;
formObj[i] =  vote[1];
DeliveryNinja
Erm, your code is used to assign the value while I seeking to merge the vote element with document so it would look like document.forms[0].vote[1]
proyb3