+ 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?
8 Antworten
+ 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!
+ 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));
+ 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.
+ 2
Do you mean storing DOM elements in an array? If yes, that is possible.
+ 2
Airree yes.
something like this:
var x = [
document.getEl...ById("elem"),
doc...("ele2"),
doc....("elem3),
]
+ 2
Yes, you can do that, I don't see why it wouldn't be possible
+ 2
Paul K Sadler ya, i can only give one upvote and i 98% of the time do upvote answers.
+ 1
Paul K Sadler if i understood what you said right, _ upvote answers too _(?).
I do take time to upvote answers btw. ⏱