0
    $(window).load(function () {
      let max = new Date().getFullYear(),
        min = max - 10
      for (let i = max; i >= min; i--) {
        let opt = new Option()
        $(opt).html(i)
        $(opt).val()
        $('#type-year').append(opt)
      }
    })
    const yearSelected = $('#type-year option:selected')
    $("#year-value").html(yearSelected.text())
    console.log(yearSelected.val())

I'm trying to set the values to numbers 0 - 10. For example, the value of 2020 should be 0, the value of 2019 should be 1, value of 2018 should be 2, and on.

Mike
  • 11
  • 2
  • You are aware that half your logic is outside of the load event handler, yes? – Taplar Apr 07 '20 at 21:57
  • Also `$(opt).val()` doesn't do anything that persists. – Taplar Apr 07 '20 at 21:58
  • Yes, that is fine. that its outside the load handler – Mike Apr 07 '20 at 22:22
  • Is there already a selected option before the load event fires? Otherwise your outer logic will happen before the new options are added. – Taplar Apr 07 '20 at 22:24
  • There are so many questions and answers here about setting select/option values with jQuery, do none of them help? [Here's one](https://stackoverflow.com/questions/1801499/how-to-change-options-of-select-with-jquery) that shows how to build a set of options (which you are doing) and how to set the option values (which is your question). – Don't Panic Apr 08 '20 at 00:14

0 Answers0