I am completely new to JavaScript, so far i have been able to learn how to get values from my select drop down. However one other thing I would like to do is have a default value which gets displayed when a user visits the page. Then change with the select action. Currently I get no value displayed until after selecting from the drop down. I have tried to search but i have not found any easy to understand guide on how to implement this. See my code below
function val() { d = document.getElementById("select_year").value; document.getElementById("demo").innerHTML = d; }
<select onchange="val()" id="select_year"> <option selected value="2017">2017</option> <option value="2016">2016</option> <option value="2015">2015</option> </select> <p id="demo"></p>
Seems like you have just to call your function.
function val() { d = document.getElementById("select_year").value; document.getElementById("demo").innerHTML = d; } val();
<select onchange="val()" id="select_year"> <option selected value="2017">2017</option> <option value="2016">2016</option> <option value="2015">2015</option> </select> <p id="demo"></p>
Just make a small change.
<option selected="selected" value="122"></option>
this will be good.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.