So I am creating a form that has a drop down for years, it currently has 3 options:
2021-2022
2022-2023
2023-2024
This element has the ID value of AidYear
Later on in the form we ask the question "Where you born before January 1, YYYY"
I need the YYYY to populate with a different value depending on what option they chose for the original drop down:
- IF 2021-2022 was selected then YYYY should display a valye of 1998
- IF 2022-2023 was selected then YYYY should display a valye of 1999
- IF 2023-2024 was selected then YYYY should display a valye of 2000
So the final result is that the person using the form should see the question like:
"Where you born before January 1, 1999"
And prior to them selecting an option in the dropdown I need it to default to 1998 like:
"Where you born before January 1, 1998"
and simply update to the new year once they select an option.
Because of the nature of the form I need to do this all in a single line.
So far I have not had luck getting it to return anything.
Were you born before January 1,
<strong id="birthbefore1"></strong>
<script>
var birthdate2 = document.getElementById("AidYear");
if (birthdate2 = "") {"1998";}
if (birthdate2 = "2021-2022") {"1998";}
if (birthdate2 = "2022-2023") {"1999";}
if (birthdate2 = "2023-2024") {"2000";}
var p = document.getElementById("birthbefore1");
p.innerHTML = birthdate2;
</script>
I get zero results or when playing with some differences it sometimes results in
[object HTMLSelectElement]
3
Answers
the values in the if statements aren’t being returned or assigned to any value. if you change your code to this:
you should get the expected result
You’re on the right track. You just need to add onchange event to AidYear, to trigger the updateBirthYear.
You are comparing the strings using the asigning operator
=
instead of the using comparison operator==
or===
, also I included anonchange
event listener that calls updateYear(this.value), which passes the currently selected value to the function