Jump to content

Set a dropdown element


Hosea
 Share

Recommended Posts

Hi can you help me with a Javascript code, I have two dropdowns, Flare Priority with options: 1,2,3 and 4; And Change Category with options: Scheduled/Normal, Standard Change, Emergency and Expedited. what needs to happen is, When I choose Flare Priority option 1, the value in the second Dropdown has to change to Emergency. Thanks in advance

Link to comment
Share on other sites

First, give your dropdowns some IDs so JavaScript knows how to talk to them. <select id="flarePriority"> and <select id="changeCategory"> for example. Then, create this JavaScript function.

function changePriority(menu) {     var menu2 = document.getElementById('changeCategory');      // check to see if the option has a value of 1     if (menu.options[menu.selectedIndex].value == 1) {          // change the option for changeCategory (assuming Emergency is option 3)          menu2.options[2].selected = true;     }}

Lastly, add the function to the onchange event on flarePriority.

<select id="flarePriority" onchange="changePriority(this)">

Link to comment
Share on other sites

IF possible, if you put the options from the first, in the same order as the the second, you could use the position order from first to select required option from the same position from second, OR again if possible, set the value attribute of first to point to the option position required in second dropdown. This would reduce the size of code required, instead of having to use lots of if else conditions combination's to target specific options from second dropdown.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...