How To Select Unique Values From Multiple Dropdown Lists Using Javascript? May 17, 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 Remove Hash From URL, Load The Page At Hash-less URL, Then Add Hash To URL Without Reloading The Page I'm working on a website that uses AJAX loading with so… How To Enforce User Must Scroll Pdf Within Iframe I have a pdf file within iframe. I want user to scroll must… Moment JS Subtract Method Returns 12 Hour Difference When There Is Only Zero Hour Left I want to display remaining hours, minutes and seconds to a… HTML5 Image Drag Event - CtrlKey Remains False In Firefox 29 This works great in Chrome, but what do I need to change to… Selenium Webdriver: Firefox Headless Inject Javascript To Modify Browser Property I'm trying to figure out how is possible to use seleniu… Typeof Foo['bar'] !== 'undefined' Vs. 'bar' In Foo What's the difference between the return values of thes… How To Send Uploaded File From Javascript To Controller In MVC? In my MVC, i have a view and that contains one file upload … "'localStorage' Is Null Or Not An Object" Error In IE8 I have this string which i'm trying to store and get to… Making Custom Checkboxes Work With Css - Select Or Check Not Working I'm not able to select a checkbox and see a check in it… Change Parent Component State From Child Component I know that the question with this title has already been a… December 2024 (1) November 2024 (36) October 2024 (74) September 2024 (22) August 2024 (219) July 2024 (184) June 2024 (429) May 2024 (688) April 2024 (448) March 2024 (858) February 2024 (908) January 2024 (835) December 2023 (890) November 2023 (379) October 2023 (581) September 2023 (315) August 2023 (354) July 2023 (281) June 2023 (349) May 2023 (204) April 2023 (119) March 2023 (148) February 2023 (180) January 2023 (280) December 2022 (135) November 2022 (236) October 2022 (172) September 2022 (166) August 2022 (482) July 2022 (310) June 2022 (286) Menu Halaman Statis Beranda © 2022 - javascript spravka