0

I'm making a function that takes two inputs and makes a select box from their contents but when I run the code it doesn't work and doesn't give me any errors.
When I run the function by itself and give it the inputs manually it seems to work just fine.

The code:

var start = parseInt(document.getElementById("start")),
  end = parseInt(document.getElementById("end")),
  button = document.getElementById("button");

button.onclick = function(x, y) {
  "use strict";
  var years;
  document.write("<select>");
  for (years = start; years <= end; years++) {
    document.write("<option value=\"" + years + "\">" + years + "</option>");
  };
  document.write("</select>");
};
<body>
  <input id="start" type="text" />
  <input id="end" type="text" />
  <button id="button">Make Select Box</button>
</body>

3
  • What are those x and y parameters? Commented May 8, 2018 at 10:25
  • 1
    On a side note, please get used to new ways of appending elements to DOM. document.write is not a good way to do this. Commented May 8, 2018 at 10:25
  • You need to access the input values inside the event handler (i.e. every time when the button is clicked), not just once at the beginning Commented May 8, 2018 at 10:26

1 Answer 1

1
var start = parseInt(document.getElementById("start"))

This part is parsing whole HTML Element. You have to extract its value:

var start = parseInt(document.getElementById("start").value)

This also should happen inside the onclick callback. If you'll assign this value before clicking - it'll not change at all.

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.