How To Select Unique Values From Multiple Dropdown Lists Using Javascript? April 21, 2024 Post a Comment I am building an online registration form. In the web form, there are six dropdown lists which is as follows : ).value, document.getElementById('hssub2').value, document.getElementById('hssub3').value, document.getElementById('hssub4').value, document.getElementById('hssub5').value, document.getElementById('hssub6').value ]; for(var i=0; i<=5; i++){ var c = valCounter[othercodes[i]] = (valCounter[othercodes[i]] || 0) + 1; if(c > 1){ document.getElementById("submit").setAttribute("disabled","disabled"); // so that it stops form submission;document.getElementById("notification").innerHTML = elt.options[elt.selectedIndex].text + " Subject Already Choosen!"; returnfalse; } } document.getElementById("notification").innerHTML = ""; document.getElementById("submit").removeAttribute("disabled"); // so that it allows form submission again; } CopySolution 2: First, I would suggest using a default option like "Select subject" whose value is "null" or 0 - something that can be easily discarded. This way there are no default selected values. Second, I would highly recommend not putting the event handlers in your markup. It's a real mess to maintain that.<selectname="hssub1"id="hssub1"><optionvalue=null>Select</option> //added default option <optionvalue="1">Bengali</option><optionvalue="2">English</option><optionvalue="3">Hindi</option><optionvalue="4">Physics</option><optionvalue="5">Chemistry</option><optionvalue="6">Mathematics</option></select><selectname="hssub2"id="hssub2"><optionvalue=null>Select</option><optionvalue="1">Bengali</option><optionvalue="2">English</option><optionvalue="3">Hindi</option><optionvalue="4">Physics</option><optionvalue="5">Chemistry</option><optionvalue="6">Mathematics</option></select><selectname="hssub3"id="hssub3"><optionvalue=null>Select</option><optionvalue="1">Bengali</option><optionvalue="2">English</option><optionvalue="3">Hindi</option><optionvalue="4">Physics</option><optionvalue="5">Chemistry</option><optionvalue="6">Mathematics</option></select><selectname="hssub4"id="hssub4"><optionvalue=null>Select</option><optionvalue="1">Bengali</option><optionvalue="2">English</option><optionvalue="3">Hindi</option><optionvalue="4">Physics</option><optionvalue="5">Chemistry</option><optionvalue="6">Mathematics</option></select><selectname="hssub5"id="hssub5"><optionvalue=null>Select</option><optionvalue="1">Bengali</option><optionvalue="2">English</option><optionvalue="3">Hindi</option><optionvalue="4">Physics</option><optionvalue="5">Chemistry</option><optionvalue="6">Mathematics</option></select><selectname="hssub6"id="hssub6"><optionvalue=null>Select</option><optionvalue="1">Bengali</option><optionvalue="2">English</option><optionvalue="3">Hindi</option><optionvalue="4">Physics</option><optionvalue="5">Chemistry</option><optionvalue="6">Mathematics</option></select><divid="notification"></div><buttontype="button"name="submit"id="submit">Save and Next</button>CopyHere's the Javascript -fairly straightforward, but you can ask in case of any questions:var selects = document.querySelectorAll('select'), notify = document.getElementById('notification'); functiongetOthers(current){ var values = []; for(var i=0;i<selects.length;i++){ if(selects[i].value!='null' && selects[i]!=current) values.push(selects[i].value); } return values; } functioncheckUnique(){ if(this.value && getOthers(this).indexOf(this.value)>-1){ notify.innerText = 'You already selected that'; this.value = null; } } for(var i=0;i<selects.length;i++) selects[i].onchange = checkUnique; document.getElementById('submit').onclick = function(){ var values = getOthers(); // will return all selected values this timeif(values.length < 6){ notify.innerText = 'select all six'; returnfalse; } notify.innerText = ''; returntrue; } CopyA fiddle: http://jsfiddle.net/p699m9x7/Solution 3: First add new option to lists with an value you can easily recognize like this:<select name="hssub1" id="hssub1" onchange="checkUnique(this.id);"> <option value="null">Select</option> <option value="1">Bengali</option> Copythen add if condition before the conditions you have now and then the loop will be like this:for(var i=0; i<=5; i++){ if(i != elementIDsuffix){ if(othercodes[i] === "null"){ //check whether anything is nulldocument.getElementById("notification").innerHTML = "Select in all lists to continue"; // if null print this }elseif(othercodes[i] == hssubcode){ document.getElementById("submit").setAttribute("disabled","disabled"); document.getElementById("notification").innerHTML = elt.options[elt.selectedIndex].text + " Subject Already Choosen!"; returnfalse; break; } else{ document.getElementById("notification").innerHTML = ""; document.getElementById("submit").removeAttribute("disabled"); } } } Copybut with this at first time you enter the page the button will not disable. So for that call this function on body onload event;<bodyonload="checkUnique('hssub1');">CopyI think this will help you to continue with minimal changes to your existing codeSolution 4: There's this JavaScript snippet on github. Just a 1.4 kB js file. Just include jquery before including the script and you should be good to go.https://github.com/akhilnaik/unique.jsYou need to give a class for all the < select > tags you want to have unique values Here I'm using class='abc' as example.<selectclass='abc'name="hssub1"id="hssub1"onchange="checkUnique(this.id);"><optionvalue="null">Select One</option><optionvalue="1">Bengali</option><optionvalue="2">English</option><optionvalue="3">Hindi</option><optionvalue="4">Physics</option><optionvalue="5">Chemistry</option><optionvalue="6">Mathematics</option></select><selectclass='abc'name="hssub2"id="hssub2"onchange="checkUnique(this.id);"><optionvalue="null">Select One</option><optionvalue="1">Bengali</option><optionvalue="2">English</option><optionvalue="3">Hindi</option><optionvalue="4">Physics</option><optionvalue="5">Chemistry</option><optionvalue="6">Mathematics</option></select> and so on ..... CopyAnd call the constructor of Unique on window.onload like thisvar k = new Unique('.abc','null');//u need to have a default null valueCopyAnd thats it everything is taken care of. The selected values will automatically be disabled.Dont forget to include JQuery before including unique.js Hope this helps.Solution 5: I don't know if this fits your needs, but if you want to get a warning, if any value is already chosen, you could do it like that:functioncheckUnique(elementID) { var values = []; var elements = document.getElementsByTagName('select'); for (var i=0, l=elements.length; i < l; i++) { if (document.getElementById(elementID).value === elements[i].value && document.getElementById(elementID) != elements[i]) { console.log('not unique'); } } } CopyInstead of console.log('not unique') you could do your warnings. Share Post a Comment for "How To Select Unique Values From Multiple Dropdown Lists Using Javascript?" Top Question Adding CSS Border Changes Positioning In HTML5 Webpage I'm having a problem with the page elements moving when… Set CSS Width To 100% + Right Border Is Missing? I set a div's width to 100% of the window. When I apply… 2 Side By Side Divs Centered? Well i have 2 divs that i want to be side by side and align… SharePoint 2013 - Display The Detail From A List Item In A Branded Page, NOT The Default SharePoint Details Page I am a COMPLETE beginner when it comes to SharePoint! So, f… Weird Dark Border :after Css Arrow In Firefox In an attempt to make an arrow in pure CSS for my tooltip, … Building A Wysiwyg Editor I need to build a wysiwyg editor for a project I am working… How To Add Vertical Sub-menu To Horizontal Menu Using Css And Html? I want to add a vertical sub-menu to my horizontal menu. I … Zend Html5 Form Element Types Is there any easy way to get HTML5 Form elements into a Zen… What Are The Flaws , One Must Care In File Upload I have a Linux server , i am having an upload image in my w… Html Parsing With Jsoup I am trying to parse the html of the following URL: http://… December 2024 (1) November 2024 (39) October 2024 (60) September 2024 (16) August 2024 (372) July 2024 (341) June 2024 (684) May 2024 (1293) April 2024 (819) March 2024 (1495) February 2024 (1638) January 2024 (1341) December 2023 (1383) November 2023 (417) October 2023 (640) September 2023 (314) August 2023 (341) July 2023 (272) June 2023 (356) May 2023 (185) April 2023 (154) March 2023 (144) February 2023 (181) January 2023 (256) December 2022 (134) November 2022 (215) October 2022 (177) September 2022 (163) August 2022 (294) July 2022 (90) Menu Halaman Statis Beranda © 2022 - Html5 stackoverflow Examples