I have a webpage using 4 search box’s in 2 in header & 2 in the body of homepage. Both Header form & form in body of homepage are the same and query the same search page.
My Question is how could I link dropdowns so that when one changes the others follow.
At the moment I am using php to switch to appropriate categories on arrival to page(s).
eg:
<option value='-1' <?php if ($cat == "-1") {print("selected");}?>>All Categories</option>
<option value='0'<?php if ($cat == "0") {print("selected");}?>>Category One</option>
<option value='1' <?php if ($cat == "1") {print("selected");}?>>Category Two</option>
Which works great when arriving on page(s) after query.
However Because there are 4 forms on my home page I was hoping to somehow dynamically when user changes one of the < select >< options > on page then the other < select >< options > in header and other places all change to same value also?
Any ideas?
Advertisement
Answer
Suppose you have 2 ‘Select’ html elements like below:
test.html
<html> <head> <script src="connectSelect.js"></script> </head> <body onload="connectSelect('first', 'second')"> <select id="first"> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select> <select id="second"> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select> </body> </html>
Just use the already made ‘connectSelect’ function on body load passing the ids of the ‘Select’ elements you want to connect.
Include the connectSelect.js file in the header. Below is the code for that:
connectSelect.js
function connectSelect(id1, id2) { var select1 = document.getElementById(id1); var select2 = document.getElementById(id2); var i, val1, text1, val2, text2, errText; //to check whether both the select elements are of same length or not if (select1.length != select2.length) { alert("connectSelect Function Error: Both the 'Select' elements should have same number of options!"); return; } //after assuring both the select elements to be of same length //to check whether both the select elements have same value and text for (i = 0; i < select1.length; i++) { val1 = select1.options[i].value; text1 = select1.options[i].innerHTML; val2 = select2.options[i].value; text2 = select2.options[i].innerHTML; if (val1 != val2 || text1 != text2) { errText = "Both the 'Select' elements should have same options with same value and text!"; errText += "n"; errText += "n"; errText += "First mismatch: Option " + (i+1); alert("connectSelect Function Error: " + errText); return; } } //after assuring both the select elements to be same select1.addEventListener("change", function(){ var index = this.selectedIndex; select2.options[index].selected = true; }); select2.addEventListener("change", function(){ var index = this.selectedIndex; select1.options[index].selected = true; }); }
I have inserted the code snippet also. Try it.
function connectSelect(id1, id2) { var select1 = document.getElementById(id1); var select2 = document.getElementById(id2); var i, val1, text1, val2, text2, errText; //to check whether both the select elements are of same length or not if (select1.length != select2.length) { alert("connectSelect Function Error: Both the 'Select' elements should have same number of options!"); return; } //after assuring both the select elements to be of same length //to check whether both the select elements have same value and text for (i = 0; i < select1.length; i++) { val1 = select1.options[i].value; text1 = select1.options[i].innerHTML; val2 = select2.options[i].value; text2 = select2.options[i].innerHTML; if (val1 != val2 || text1 != text2) { errText = "Both the 'Select' elements should have same options with same value and text!"; errText += "n"; errText += "n"; errText += "First mismatch: Option " + (i+1); alert("connectSelect Function Error: " + errText); return; } } //after assuring both the select elements to be same select1.addEventListener("change", function(){ var index = this.selectedIndex; select2.options[index].selected = true; }); select2.addEventListener("change", function(){ var index = this.selectedIndex; select1.options[index].selected = true; }); }
<html> <head> <script src="test.js"></script> </head> <body onload="connectSelect('first', 'second')"> <select id="first"> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select> <select id="second"> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select> </body> </html>
Hope it helped.
EDIT: For a better solution, please check this (Source: From comments of this question)