How To Select Unique Values From Multiple Dropdown Lists Using JavaScript? March 22, 2023 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 Trying To Grab URL Parameters, And POST Them With Form I am trying to get a landing page grab the URL parameters. … Are Deferred Scripts Executed Before DOMContentLoaded Event? Upon defer attribute MDN says: This Boolean attribute is s… How To Fetch Data Over Multiple Pages? My project is based on React, redux, redux-saga, es6 and I … JavaScript Image Slider That Will Allow More Slides To Be Added After Page Load I am currently building an image slider page. The slider lo… JavaScript: Hide Button Link If Input Field Is Empty I am trying to hide a button link (id='btnsubmit') … Auto Width Scroll Bar In Google Chart I using the Google Chart for in my HTML5 Project. which tak… Persistent HTML Outside Of JQuery Mobile Pages I want to include a bit of HTML outside of the element on … CSS Column-count Elements Jumping Across Columns I'm trying to get a dynamic amount of elements to show … How To Make Keypress Event Of Asp Textbox? Hi all i am writing calculation of varius asp textbox contr… LastModified() Function Returns Current Date And Time My question is : why when I use 'document.lastModified&… December 2024 (1) November 2024 (39) October 2024 (63) September 2024 (22) August 2024 (215) July 2024 (181) June 2024 (437) May 2024 (706) April 2024 (446) March 2024 (907) February 2024 (870) January 2024 (824) December 2023 (929) November 2023 (375) October 2023 (526) September 2023 (294) August 2023 (313) July 2023 (258) June 2023 (340) May 2023 (210) April 2023 (133) March 2023 (138) February 2023 (114) January 2023 (266) December 2022 (133) November 2022 (234) October 2022 (165) September 2022 (169) August 2022 (484) July 2022 (308) June 2022 (281) Menu Halaman Statis Beranda © 2022 - javascript dox