1

I have just started learning vanilla JavaScript and I'm trying to create a drop-down menu targeting the HTML snippet.

I attempted to use the loop method to create 25 options but it only displays 1.

My code:

<select name="25dropdown" id="25dropdown">

</select>

<script>

function dropDown(){
for (y = 1; y < 26; y++) {
    document.getElementById("25dropdown").innerHTML = ("<option value =" + y + ">" + y + "</option>");
    }
}
dropDown();

</script>
1

1 Answer 1

2

In each iteration you are replacing the HTML, but you have to keep the previously added HTML as well. Try += instead of =

<select name="25dropdown" id="25dropdown">

</select>

<script>

function dropDown(){
  for (y = 1; y < 26; y++) {
    document.getElementById("25dropdown").innerHTML += ("<option value =" + y + ">" + y + "</option>");
  }
}
dropDown();

</script>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.