最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - getElementById(array[x])? - Stack Overflow

matteradmin3PV0评论

I'm trying to put an array in to getElementById for a loop purpose. It seems to be not working, how can I do this?

Edit: Sorry folks It says undefined.

    var lol=new Array( "test", "test2" );

var x = 0;
while( x == 4 ) {
    number = parseInt(document.getElementById(lol[x]).value);
    x++;
}

And i have inputs id named test and test2.

I'm trying to put an array in to getElementById for a loop purpose. It seems to be not working, how can I do this?

Edit: Sorry folks It says undefined.

    var lol=new Array( "test", "test2" );

var x = 0;
while( x == 4 ) {
    number = parseInt(document.getElementById(lol[x]).value);
    x++;
}

And i have inputs id named test and test2.

Share Improve this question edited Feb 1, 2010 at 17:51 Strawberry asked Feb 1, 2010 at 17:37 StrawberryStrawberry 68k58 gold badges156 silver badges206 bronze badges 7
  • 1 It seems to be not working : Any error messages,unexpected behavior, etc? Code would also help... – Felix Kling Commented Feb 1, 2010 at 17:40
  • 9 <sillyRandomGuess reason="NoCodeProvided">You have a syntax error on line 31</sillyRandomGuess> – Josh Stodola Commented Feb 1, 2010 at 17:40
  • Code snippet + error message = diagnosable problem. – David Berger Commented Feb 1, 2010 at 17:40
  • @Josh your phraseology is hilariouser than mine. – David Berger Commented Feb 1, 2010 at 17:41
  • getElementById() can't deal with the array. Just run through the array and do a single getElementById() on each member. – Pekka Commented Feb 1, 2010 at 17:46
 |  Show 2 more ments

3 Answers 3

Reset to default 8

Your while loop only works if x==4. Change this to:

while(x < lol.length)

To loop through all the elements in the array. Better yet, this will condense your loop:

var lol=new Array( "test", "test2" );
for( var x = 0; x < lol.length; x++ ) {
    number = parseInt(document.getElementById(lol[x]).value);
}

Try taking your array out of the quotes...

document.getElementById(lol[x]).value

The quotes turn it into a static string "lol[x]", when you want the value of the lol array at x index.

This replaces my earlier, less informed answer.

Hope this helps

You say you have number = parseInt(document.getElementById("lol[x]").value);

  1. "lol[x]" is a string with that literal value, not the value that lol holds at index x. Use getElementById(lol[x])

  2. parseInt may do unexpected things when you don't pass a radix. Use something like parseInt(document.getElementById(lol[x]).value, 10)

Finally, you aren't checking whether the element exists. Do something like:

var element = document.getElementById(lol[x]);
if (element) {
  number = parseInt(element.value, 10);
} else {
  // handle error or throw exception
}
Post a comment

comment list (0)

  1. No comments so far