1

here is my code. I can't make the alert works.

var chooseObject = document.getElementsByTagName("input");
var objectLength = chooseObject.length;
var hobbyArray = [];
    //hobbyArray("aaa");
    //hobbyArray("bbb");
    //alert(hobbyArray); This one works.
for(var i=0;i<=objectLength;i++){
    if((chooseObject[i].type=="checkbox")&&(chooseObject[i].checked==true)){
         //alert(chooseObject[i].value); This one works.
         hobbyArray.push(chooseObject[i].value); }
}
alert(hobbyArray);

If I do the top alert(I comment already) it works. If I alert chooseObject[i].value in the for loop, its fine. But if I do with array, it failed. Could someone help me?

2
  • What do you expect to get with alert(hobbyArray)? Commented Nov 12, 2012 at 21:33
  • alerting an array is not exactly the way to go.. But most browser should give you some kind of content in your alert. Try alert(hobbyArray.join(', ')); Commented Nov 12, 2012 at 21:34

1 Answer 1

2

You have a silly error on your for statement. This:

for(var i=0;i<=objectLength;i++)

should be:

for(var i=0;i<objectLength;i++)

The extra iteration is causing a TypeError when you check the element's properties (chooseObject[objectLength] gives undefined, which has no properties).

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.