there is a link and a select item, tell me how to programmatically when you click on a link to make it as if the user selected the select element (i.e. clicked on it with the mouse)
- assign the necessary value to the select - Grundy
- @Grundy set the selected attribute. - pepel_xD
- not. set property value - Grundy
|
1 answer
function selectChange(val){ let select = document.getElementsByTagName("select")[0]; select.selectedIndex = val; } <select id="select"> <option value="1" selected>1</option> <option value="2">2</option> <option value="3">3</option> </select> <button onclick="selectChange(0);">1</button> <button onclick="selectChange(1);">2</button> <button onclick="selectChange(2);">3</button> - Be careful. With this approach, events like
onclickoronchangethat the <select> tag does not have to work. - Stepan Kasyanenko
|