+ 2

Can I use variables inside getelementbyid()?

Consider there is a div element with id "div_id" Let variable A = div_id. then is getElementById("div_id") equivalent to getElementById(A)?

27th Apr 2018, 8:13 PM
Adnan Elachola
Adnan Elachola - avatar
2 Answers
+ 1
Hi Adnan yes, variable needs to be a string but you probably want to do something like this getElementById("div_id"+a); So you get div_id1, div_id2. etc
27th Apr 2018, 8:22 PM
Mike Choy
Mike Choy - avatar
+ 1
thank you Mike
27th Apr 2018, 8:24 PM
Adnan Elachola
Adnan Elachola - avatar