Seif Tarek
Seif Tarek

Reputation: 11

JavaScript Functions And Events

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>

Upvotes: 0

Views: 57

Answers (1)

hsz
hsz

Reputation: 152216

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.

Upvotes: 1

Related Questions