Plain Javascript Delete Item From Drop Down Select February 18, 2024 Post a Comment I got the two following dropdown lists and I want to remove the option chosen in either one. So if option 1 is chosen, it will not be there in list 2. ; } document.getElementById("first").addEventListener("change", function() { show_all(); document.querySelector("#second option[value='"+this.value+"']").style.display="none"; });Copy<selectclass='form-dropdown'name='first'id='first'><optionvalue='none'selected>No Choice</option><optionvalue='Workshop 1'> Workshop 1 </option><optionvalue='Workshop 2'> Workshop 2 </option><optionvalue='Workshop 3'> Workshop 3 </option><optionvalue='Workshop 4'> Workshop 4 </option><optionvalue='Workshop 5'> Workshop 5 </option></select><selectclass='form-dropdown'name='second'id='second'><optionvalue='none'selected>No Choice</option><optionvalue='Workshop 1'> Workshop 1 </option><optionvalue='Workshop 2'> Workshop 2 </option><optionvalue='Workshop 3'> Workshop 3 </option><optionvalue='Workshop 4'> Workshop 4 </option><optionvalue='Workshop 5'> Workshop 5 </option></select>CopyIf you want to avoid the duplicate selection in the both lists you could use :functionshow_all(select_name){ var second_options = document.querySelectorAll("#"+select_name+" option"); for(var i=0;i<second_options.length;i++){ second_options[i].style.display="block"; } } document.getElementById("first").addEventListener("change", function() { show_all("second"); document.querySelector("#second option[value='"+this.value+"']").style.display="none"; }) document.getElementById("second").addEventListener("change", function() { show_all("first"); document.querySelector("#first option[value='"+this.value+"']").style.display="none"; })Copy<selectclass='form-dropdown'name='first'id='first'><optionvalue='none'selected>No Choice</option><optionvalue='Workshop 1'> Workshop 1 </option><optionvalue='Workshop 2'> Workshop 2 </option><optionvalue='Workshop 3'> Workshop 3 </option><optionvalue='Workshop 4'> Workshop 4 </option><optionvalue='Workshop 5'> Workshop 5 </option></select><selectclass='form-dropdown'name='second'id='second'><optionvalue='none'selected>No Choice</option><optionvalue='Workshop 1'> Workshop 1 </option><optionvalue='Workshop 2'> Workshop 2 </option><optionvalue='Workshop 3'> Workshop 3 </option><optionvalue='Workshop 4'> Workshop 4 </option><optionvalue='Workshop 5'> Workshop 5 </option></select>CopySolution 2: Just have a look at this code , working fine for me<html><selectclass='form-dropdown'name='first'id='first'onchange="fun1(this.value)"><optionvalue='none'selected>No Choice</option><optionvalue='Workshop 1'> Workshop 1 </option><optionvalue='Workshop 2'> Workshop 2 </option><optionvalue='Workshop 3'> Workshop 3 </option><optionvalue='Workshop 4'> Workshop 4 </option><optionvalue='Workshop 5'> Workshop 5 </option></select><selectclass='form-dropdown'name='second'id='second'onchange="fun2(this.value)"><optionvalue='none'selected>No Choice</option><optionvalue='Workshop 1'> Workshop 1 </option><optionvalue='Workshop 2'> Workshop 2 </option><optionvalue='Workshop 3'> Workshop 3 </option><optionvalue='Workshop 4'> Workshop 4 </option><optionvalue='Workshop 5'> Workshop 5 </option></select></html><script>functionfun1(val){ updateFun2(val) } functionfun2(val){ updateFun1(val) } functionupdateFun2(val){ selectobject=document.getElementById('second'); for (var i=0; i<selectobject.length; i++){ if (selectobject.options[i].value == val ) selectobject.remove(i); } } functionupdateFun1(val){ selectobject=document.getElementById('first'); for (var i=0; i<selectobject.length; i++){ if (selectobject.options[i].value == val ) selectobject.remove(i); } } </script>CopySolution 3: HTML CODE :-<selectclass='form-dropdown'name='first'id='first'><optionvalue='none'selected>No Choice</option><optionvalue='Workshop 1'> Workshop 1 </option><optionvalue='Workshop 2'> Workshop 2 </option><optionvalue='Workshop 3'> Workshop 3 </option><optionvalue='Workshop 4'> Workshop 4 </option><optionvalue='Workshop 5'> Workshop 5 </option></select><selectclass='form-dropdown'name='second'id='second'><optionvalue='none'selected>No Choice</option><optionvalue='Workshop 1'> Workshop 1 </option><optionvalue='Workshop 2'> Workshop 2 </option><optionvalue='Workshop 3'> Workshop 3 </option><optionvalue='Workshop 4'> Workshop 4 </option><optionvalue='Workshop 5'> Workshop 5 </option></select>CopyJavascript Code :-var first = document.getElementById('first'); var second = document.getElementById('second'); var optionOld = document.createElement("option"); first.addEventListener('change', function() { var option = second.querySelector('option[value="' + this.value + '"]'); if(optionOld.text != '') { second.add(optionOld); } optionOld = option; second.removeChild(option); }); CopyI have changed some code in adeneo JSfiddler.Please check it working on my environment.Also Find fiddler https://jsfiddle.net/xh3qb8r6/1/Thanks.Solution 4: You should use change event listener on the first dropdown, check the value of it, and remove the element from the second dropdown which has the same value:document.getElementById("first").addEventListener("change", event=> { if (event.target.value === "none") returndocument.querySelector(`#second [value="${event.target.value}"]`).remove() }) CopySee demo on JS Bin.Solution 5: UpdateI realized that just using the index wasn't working right since #second would diminish in length and it's index would change as well. So I added a for loop to iterate through #second and upon a match of values between #first and #second options, the index is used to target the matching option and then it's removed.on the change event...Get the index of the #first<option> selected.Store index in var, and...Use .remove(index) targeting #secondvar first = document.getElementById('first'); first.addEventListener('change', function(e) { var second = document.getElementById('second'); var selected = this.options[this.selectedIndex].value; for (var i=0; i<second.length; i++){ if (second.options[i].value == selected ) second.remove(i); } }, false);Copy<selectclass='form-dropdown'name='first'id='first'><optionvalue='none'selected>No Choice</option><optionvalue='Workshop 1'>Workshop 1</option><optionvalue='Workshop 2'>Workshop 2</option><optionvalue='Workshop 3'>Workshop 3</option><optionvalue='Workshop 4'>Workshop 4</option><optionvalue='Workshop 5'>Workshop 5</option></select><selectclass='form-dropdown'name='second'id='second'><optionvalue='none'selected>No Choice</option><optionvalue='Workshop 1'>Workshop 1</option><optionvalue='Workshop 2'>Workshop 2</option><optionvalue='Workshop 3'>Workshop 3</option><optionvalue='Workshop 4'>Workshop 4</option><optionvalue='Workshop 5'>Workshop 5</option></select>Copy Share Post a Comment for "Plain Javascript Delete Item From Drop Down Select" Top Question Split Comma Separated String By Comma Need to split the string containing country names separated… Changing Font Size Options On WYSIWYG Editor? So I have a bootstrap wysiwyg editor from a jqueryscript.ne… What Is The Correct Way To "serialize" Functions In Javascript For Later Use I have a 'library' of objects that I want to load o… Bootstrap Counter Start On Display So I found this codepen tutorial that helped me get number … Select And Unselect All Checkboxes I'm not able to implement these things,In this page I w… Undo Redo For Fabric.js I'm trying to add undo/redo functionality to my Fabric.… Three.js - Scale Model With Scale.set() Or Increase Model Size? What is the best practise for scaling 3d models in Three.js… How Does Node.JS Work As Opposed To PHP? I'm thinking of switching from PHP to using Node.js for… Google Maps Api V3: Exclude Single Marker From Clustering I am using the google maps api and using grid clustering fo… Logic For The Next Button For The Questionnaire? I am beginner in AngularJS and facing some issues. I am try… December 2024 (1) November 2024 (37) October 2024 (63) September 2024 (20) August 2024 (385) July 2024 (340) June 2024 (706) May 2024 (1296) April 2024 (848) March 2024 (1553) February 2024 (1722) January 2024 (1374) December 2023 (1396) November 2023 (450) October 2023 (631) September 2023 (335) August 2023 (350) July 2023 (265) June 2023 (378) May 2023 (204) April 2023 (134) March 2023 (143) February 2023 (183) January 2023 (280) December 2022 (131) November 2022 (247) October 2022 (168) September 2022 (167) August 2022 (500) July 2022 (314) June 2022 (260) Menu Halaman Statis Beranda © 2022 - javascript edu