1

I know there are multiple posts on this topic, and I have followed them to get to this point. However I cannot seem to get my dropdown to populate with my options. Maybe I have been staring at it for too long... any help is appreciated.

var fromYear = document.getElementById("from-year");
var toYear = document.getElementById("to-Year");
var years = ["2019","2020","2021","2022","2023","2024","2025","2026"];
for(var i = 0; i < years.length; i++) {
    var opt = years[i];
    var el = document.createElement("year");
    el.textContent = opt;
    el.value = opt;
    fromYear.appendChild(el);
    toYear.appendChild(el);
}
<div>
  <div class = "date-from">
    <select id="from-year">
      <option>from: Year</option>
    </select>
  </div>

  <div class = "date-to">
    <select id="to-year">
      <option>to: Year</option>
    </select>
  </div>
</div>
{% load static %}
<script src="{% static 'buildstats/buildStats.js' %}"></script>

I have other javascript in this .js file, and it is working correctly and populating elements on the DOM. It is just this options list that does not want to populate. The only item in the options list that populates is the default value I gave it in the HTML doc.

3
  • Where are you inserting the code? In the <head> or in the <body>? Commented Feb 7, 2019 at 11:27
  • 1
    var el = document.createElement("year"); - year isn't a valid HTML tag, I don't know if this is why the script is falling over, but surely it should be "option"? Commented Feb 7, 2019 at 11:28
  • 2
    document.getElementById("to-Year") notice the capitalised Year. Commented Feb 7, 2019 at 11:29

3 Answers 3

1

var el = document.createElement("year") is not an option, since you are working with a select element, you should use:

var el = document.createElement("option");

Also you are getting the element with document.getElementById("to-Year"), there is a typo there, since your element id is to-year (lower case).

This should make it:

var fromYear = document.getElementById("from-year");
var toYear = document.getElementById("to-year");
var years = ["2019","2020","2021","2022","2023","2024","2025","2026"];
for(var i = 0; i < years.length; i++) {
    var elfrom = document.createElement("option");
    var elto = document.createElement("option");
    elfrom.text = years[i];
    elfrom.value = years[i];
    elto.text = years[i];
    elto.value = years[i];
    fromYear.appendChild(elfrom);
    toYear.appendChild(elto);
}
<div>
  <div class = "date-from">
    <select id="from-year">
      <option>from: Year</option>
    </select>
  </div>

  <div class = "date-to">
    <select id="to-year">
      <option>to: Year</option>
    </select>
  </div>
</div>

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

2 Comments

This isn't working for the "from year". Looks like there's another error in the OP that no-one has spotted yet - you have to copy the element before appending it in a second place, else it will "move" when appended the second time.
You are right. I've adjusted the snippet so it works properly. ty
1

const fromYear = document.getElementById("from-year");
const toYear = document.getElementById("to-year");
const years = ["2019","2020","2021","2022","2023","2024","2025","2026"];

years.forEach(year => {
  fromYear.append(new Option(year, year));
  toYear.append(new Option(year, year));
});
<div>
  <div class = "date-from">
    <select id="from-year">
      <option>from: Year</option>
    </select>
  </div>

  <div class = "date-to">
    <select id="to-year">
      <option>to: Year</option>
    </select>
  </div>
</div>

Comments

1

You used the id for to-year wrong. There is no element called year which you can create, put options in createelement instead. And you cannot append one element to multiple selects. You have to create seperate elements and append both to the respective selects

var fromYear = document.getElementById("from-year");
var toYear = document.getElementById("to-year");
var years = ["2019","2020","2021","2022","2023","2024","2025","2026"];
for(var i = 0; i < years.length; i++) {
    var opt = years[i];
    var el = document.createElement("option");
    el.textContent = opt;
    el.value = opt;
    var el1 = document.createElement("option");
    el1.textContent = opt;
    el1.value = opt;
    fromYear.appendChild(el);
       toYear.appendChild(el1);
}
<div>
  <div class = "date-from">
    <select id="from-year">
      <option>from: Year</option>
    </select>
  </div>

  <div class = "date-to">
    <select id="to-year">
      <option>to: Year</option>
    </select>
  </div>
</div>

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.