I want to add options to a select tag in JavaScript | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

I want to add options to a select tag in JavaScript

I want to add options to a select. I got it working for fixed string been added but when I changed it to variables it adds the first but not subsequent ones. Included are the two versions of the webpage. https://code.sololearn.com/Wnx2YF5cXBN8/?ref=app https://code.sololearn.com/W8UiWoa4Y3hI/?ref=app

10th May 2023, 10:29 AM
Gray Adamson
Gray Adamson - avatar
3 Réponses
+ 3
Gray Adamson This also works <!DOCTYPE html> <html> <body> <select id="myList"></select> <script> const select = document.getElementById("myList"); document.body.onload = function(){ let i = 1, iMax = 3; while(i<=iMax){ let node = document.createElement("option"); node.textContent = i; select.appendChild(node); i++; } } </script> </body> </html> how option values and select action could also be set in js: https://code.sololearn.com/W8j47iKM17kf/?ref=app
10th May 2023, 11:49 AM
Bob_Li
Bob_Li - avatar
+ 2
https://code.sololearn.com/WTw82Hw987LL/?ref=app try this maybe you are using > instead of >= in the for loop
10th May 2023, 11:02 AM
Sharique
+ 2
Thanks I got it working I include my code https://code.sololearn.com/W8UiWoa4Y3hI/?ref=app
18th May 2023, 8:32 AM
Gray Adamson
Gray Adamson - avatar