+ 3

J.S. can use arrays to get us elements - document.getElemenrById(i) ??

How can I use arrays to document.getElementById? Is it even possible?

17th Jun 2019, 6:56 PM
Ginfio
Ginfio - avatar
8 Respuestas
+ 2
Ginfio Thanks for asking that question! I've noticed a lot of Sololearners take time to upvote an answer without upvoting the question. Consider that without the question, there would be no answer to upvote! Let's take the time to upvote the question too!
18th Jun 2019, 12:01 AM
Paul K Sadler
Paul K Sadler - avatar
+ 6
You can use the `.map` function to tighten up the code, it runs a function for each element in the array: let elements = [ "elem", "elem2", "elem3" ].map(id => document.getElementById(id));
17th Jun 2019, 7:08 PM
Schindlabua
Schindlabua - avatar
+ 3
Ginfio I may not have been clear. My point is that I often see answers to a question receiving upvotes and the question that they answer receiving few or no upvotes.
18th Jun 2019, 12:09 AM
Paul K Sadler
Paul K Sadler - avatar
+ 2
Do you mean storing DOM elements in an array? If yes, that is possible.
17th Jun 2019, 7:02 PM
Airree
Airree - avatar
+ 2
Airree yes. something like this: var x = [ document.getEl...ById("elem"), doc...("ele2"), doc....("elem3), ]
17th Jun 2019, 7:05 PM
Ginfio
Ginfio - avatar
+ 2
Yes, you can do that, I don't see why it wouldn't be possible
17th Jun 2019, 7:06 PM
Airree
Airree - avatar
+ 2
Paul K Sadler ya, i can only give one upvote and i 98% of the time do upvote answers.
18th Jun 2019, 12:13 AM
Ginfio
Ginfio - avatar
+ 1
Paul K Sadler if i understood what you said right, _ upvote answers too _(?). I do take time to upvote answers btw. ⏱
18th Jun 2019, 12:06 AM
Ginfio
Ginfio - avatar