I have a dropdown menu and I cannot figure out how to make a javascript function select a drop down menu option. I have tested the output of the variables and they are all correct, but it still will not select the option when clicked. Here is the function and drop down menu.
Function
function formFill(a, b, c){ theform.from.value = a; theform.to.value = b; for(var i = 0;i < document.getElementById("stateSelect").length;i++){ if(document.getElementById("stateSelect").options[i].value == c ){ document.getElementById("stateSelect").selected = true; } } }
Menu item
<select id="stateSelect" name="stateSelect"> <option value="none">(None)</option> <option value="AL">Alabama</option> <option value="AK">Alaska</option>
The <select> tab is used with <option> tab to create the simple dropdown list in HTML. After that JavaScript helps to perform operation with this list. Other than this, you can use the container tab <div> to create the dropdown list. Add the dropdown items and links inside it.
To select a <select> element, you use the DOM API like getElementById() or querySelector() . How it works: First, select the <button> and <select> elements using the querySelector() method. Then, attach a click event listener to the button and show the selected index using the alert() method when the button is clicked.
The select tag in HTML is used to create a dropdown list of options that can be selected. The option tag contains the value that would be used when selected. The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute.
Change the line that reads:
document.getElementById("stateSelect").selected = true;
to:
document.getElementById("stateSelect").selectedIndex = i;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With