I’ve seen that you can change the selected dropdown item by value but I’d like to do it by the option ID because the values are created dynamically. I am creating a questionnaire with a dropdown menu, the section with questions, and a previous/next button. The values of the dropdown are dynamically created in a php array.
The way I have it set up is similar to this:
<select id="myDropdown">
<option id="1" value="dynamicallycreated1"></option>
<option id="2" value="dynamicallycreated2"></option>
<option id="3" value="dynamicallycreated3"></option>
</select>
The select is created through php with the index of the array acting as the ID. There are previous/next buttons on the page to move back/forward from section to section. I want the dropdown to change to the appropriate item when the user clicks the next or previous button. I can easily grab the index through my current javascript so how can I change the dropdown using this index as the ID? Thanks!
I agree with Lee Taylor that you should not use 1,2,3 as Id field. You can instead use these values in
valueattribute of a dropdown likeand use jquery to select the element with value = 1 like