How To Select Unique Values From Multiple Dropdown Lists Using JavaScript? August 16, 2022 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!"; return false; } } document.getElementById("notification").innerHTML = ""; document.getElementById("submit").removeAttribute("disabled"); // so that it allows form submission again; } Copy Solution 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. <select name="hssub1" id="hssub1"> <option value=null>Select</option> //added default option <option value="1">Bengali</option> <option value="2">English</option> <option value="3">Hindi</option> <option value="4">Physics</option> <option value="5">Chemistry</option> <option value="6">Mathematics</option> </select> <select name="hssub2" id="hssub2"> <option value=null>Select</option> <option value="1">Bengali</option> <option value="2">English</option> <option value="3">Hindi</option> <option value="4">Physics</option> <option value="5">Chemistry</option> <option value="6">Mathematics</option> </select> <select name="hssub3" id="hssub3"> <option value=null>Select</option> <option value="1">Bengali</option> <option value="2">English</option> <option value="3">Hindi</option> <option value="4">Physics</option> <option value="5">Chemistry</option> <option value="6">Mathematics</option> </select> <select name="hssub4" id="hssub4"> <option value=null>Select</option> <option value="1">Bengali</option> <option value="2">English</option> <option value="3">Hindi</option> <option value="4">Physics</option> <option value="5">Chemistry</option> <option value="6">Mathematics</option> </select> <select name="hssub5" id="hssub5"> <option value=null>Select</option> <option value="1">Bengali</option> <option value="2">English</option> <option value="3">Hindi</option> <option value="4">Physics</option> <option value="5">Chemistry</option> <option value="6">Mathematics</option> </select> <select name="hssub6" id="hssub6"> <option value=null>Select</option> <option value="1">Bengali</option> <option value="2">English</option> <option value="3">Hindi</option> <option value="4">Physics</option> <option value="5">Chemistry</option> <option value="6">Mathematics</option> </select> <div id="notification"></div> <button type="button" name="submit" id="submit">Save and Next</button> Copy Here's the Javascript -fairly straightforward, but you can ask in case of any questions: var selects = document.querySelectorAll('select'), notify = document.getElementById('notification'); function getOthers(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; } function checkUnique(){ 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 time if(values.length < 6){ notify.innerText = 'select all six'; return false; } notify.innerText = ''; return true; } Copy A 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> Copy then 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 null document.getElementById("notification").innerHTML = "Select in all lists to continue"; // if null print this }else if(othercodes[i] == hssubcode){ document.getElementById("submit").setAttribute("disabled","disabled"); document.getElementById("notification").innerHTML = elt.options[elt.selectedIndex].text + " Subject Already Choosen!"; return false; break; } else{ document.getElementById("notification").innerHTML = ""; document.getElementById("submit").removeAttribute("disabled"); } } } Copy but with this at first time you enter the page the button will not disable. So for that call this function on body onload event; <body onload="checkUnique('hssub1');"> Copy I think this will help you to continue with minimal changes to your existing code Solution 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.js You need to give a class for all the < select > tags you want to have unique values Here I'm using class='abc' as example. <select class='abc' name="hssub1" id="hssub1" onchange="checkUnique(this.id);"> <option value="null">Select One</option> <option value="1">Bengali</option> <option value="2">English</option> <option value="3">Hindi</option> <option value="4">Physics</option> <option value="5">Chemistry</option> <option value="6">Mathematics</option> </select> <select class='abc' name="hssub2" id="hssub2" onchange="checkUnique(this.id);"> <option value="null">Select One</option> <option value="1">Bengali</option> <option value="2">English</option> <option value="3">Hindi</option> <option value="4">Physics</option> <option value="5">Chemistry</option> <option value="6">Mathematics</option> </select> and so on ..... Copy And call the constructor of Unique on window.onload like this var k = new Unique('.abc','null');//u need to have a default null value Copy And 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: function checkUnique(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'); } } } Copy Instead 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 Select Element Onmouseleave Event In Safari I want to catch the mouse leaving a select box with the cod… How To Dynamically Add An New Input Field On "keydown" Using JQuery? I want to have a new input field dynamically generated when… Input Type Datetime-local Is Not Working In Firefox I have an input type of datetime-local which is working fin… Block Scoping Of Let And For…of The let statement in ES2015 allows us to declare block scop… Reference Errors Thrown In Included File... Unless Calling File Includes Slow Code A funny thing happened to me while I was cleaning up some o… Getting Object Name From Inside The Class (JavaScript) I have a class and I want from inside the class to save as … Cannot Read Property 'getContext' Of Null I have a button which on click executes this function.This … What Are Alternatives To ExtJS? So what I'm looking for is a javascript framework I can… How Do I Use Data From Vue.js Child Component Within Parent Component? I have a form component where I use a child component. I wa… Link External JS File To Prestashop I'm creating a custom module in Prestashop 1.7, and I&#… December 2024 (1) November 2024 (37) October 2024 (70) September 2024 (23) August 2024 (367) July 2024 (334) June 2024 (701) May 2024 (1297) April 2024 (807) March 2024 (1552) February 2024 (1717) January 2024 (1373) December 2023 (1452) November 2023 (421) October 2023 (593) September 2023 (302) August 2023 (343) July 2023 (270) June 2023 (370) May 2023 (215) April 2023 (132) March 2023 (152) February 2023 (181) January 2023 (286) December 2022 (131) November 2022 (239) October 2022 (164) September 2022 (169) August 2022 (505) July 2022 (313) June 2022 (269) Menu Halaman Statis Beranda © 2022 - JavaScript Gen