I’ve study this thread for two days and still can’t figure out what I should do. Change <select>'s option and trigger events with JavaScript
What I am trying to do is have two dropdown selection lists, and when the user makes the selection, it passes two parameters and loads another page.
I’ve looked at something like this:
<select name="Box 1" >
<option val1 ="One" val2="One">1.1</option>
<option val1 ="One" val2="Two">1.2</option>
<option val1 ="One" val2="Three">1.3</option>
</select>
<select name="Box 2" >
<option val1 ="Two" val2="One">2.1</option>
<option val1 ="Two" val2="Two">2.2</option>
<option val1 ="Two" val2="Three">2.3</option>
</select>
The JavaScript code would be something like this:
window.location.href = 'sheet_2.html?s2_val1='+val1+'&s2_val2='+val2;
What I want to happen is that if a user clicks on either dropdown and makes a selection, the code will load the sheet_2 with data based on the two variables. I don’t want a "submit" or "go" button on sheet 1.
I think something around the "onclick" is what I want but this is my first time with a dropdown list and I don’t know if that’s what I should do.
Again, I’m very new at this, but can someone explain what I should do and help me understand why?
The code was originally written by someone else who had a bunch of buttons on the first page. I don’t know if that’s because it’s difficult to do this with dropdowns or because they were not experienced with dropdowns.
2
Answers
For values you can use html
data-
attributes. You can attach addEventListener to the parent element and in the handler use target to get the current element’s dataset :You can use data options to set the values and get them from the Select dataset object to create the url as you want.
In the end you just need to use the generated url string on the window redirect.