How To Select Unique Values From Multiple Dropdown Lists Using Javascript? February 03, 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 How To Make A Tab (i.e. Home, About, Contact) Clickable In Html? I want to make my menu button clickable so that it navigate… Transparency Of A Filled Stroke In HTML5 I'm working on a doodling app in HTML5 and I would like… Full Width List In CSS I'm trying to implement and new layout for my site. I a… How To Apply Font Family In HTML Tag Of Select Option I am applying Font family in tag but it is not working any… How To Make A Navbar Like WikiHow? I would like a navbar like WikiHow with a icon on top and t… Html Formatted Text In An Email In Java try{ String msg='Happy BirthDay Dear, '+na… Getting More Granular Diffs From Difflib (or A Way To Post-process A Diff To Achieve The Same Thing) Downloading this page and making a minor edit to it, changi… Change Text Color Inside Box On Hover So right now self teaching myself so html and CSS and I hav… Relative Path To Absolute With File:// Protocol I do scraping on a site which has similar html I also ha… Angularjs: Pass Parameter Between Controllers What i am trying to do: I have a ListView (or rather an acc… December 2024 (1) November 2024 (39) October 2024 (64) September 2024 (16) August 2024 (357) July 2024 (329) June 2024 (675) May 2024 (1285) April 2024 (750) March 2024 (1511) February 2024 (1704) January 2024 (1343) December 2023 (1361) November 2023 (435) October 2023 (564) September 2023 (318) August 2023 (329) July 2023 (281) June 2023 (350) May 2023 (230) April 2023 (144) March 2023 (142) February 2023 (163) January 2023 (237) December 2022 (132) November 2022 (212) October 2022 (175) September 2022 (161) August 2022 (293) July 2022 (98) Menu Halaman Statis Beranda © 2022 - Html5 stackoverflow Examples