I have a javascript function that needs to be triggered based on a selected option, however I would like for a default to be selected
<select class="custom-select" name="" id="timer1numbers" onchange="getSelected1()">
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8" selected>8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
How can I trigger the script without requiring a change from the user?
Just by calling document.getElementById("timer1numbers").onchange();
this, you can trigger your very first onchange
event with the selected value.
Here is the code snippet and look at the console:
function getSelected1() { var value = document.getElementById("timer1numbers").value; console.log(value); } document.getElementById("timer1numbers").onchange();
<select class="custom-select" name="" id="timer1numbers" onchange="getSelected1()"> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8" selected>8</option> <option value="9">9</option> <option value="10">10</option> </select>
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.