Hide/show The Selected Option On A Secondary Select March 26, 2024 Post a Comment I have two elements with different IDs. When the user selects a value from the first select box, I want the second select box to only display connected values. My cSolution 1: If you can use jQuery then you can always just clear and append the options to the second select. $('#ExtraField_1').change(function(){ $('#ExtraField_2').find('option').remove() if($(this).val() == '1'){ $('#ExtraField_2').append($("<option </option>").attr('value','2').text('test2')); $('#ExtraField_2').append($("<option></option>").attr('value','3').text('test3')); $('#ExtraField_2').append($("<option></option>").attr('value','4').text('test4')); } if($(this).val() == '2'){ $('#ExtraField_2').append($("<option></option>").attr('value','5').text('test5')); $('#ExtraField_2').append($("<option></option>").attr('value','6').text('test6')); $('#ExtraField_2').append($("<option></option>").attr('value','7').text('test7')); } }); Copyhttp://jsfiddle.net/8XVuv/2/using only javascript is a bit more complicated but I would still take the same approach.function createOption(otext,oValue){ var newOption = document.createElement('option'); newOption.text = otext; newOption.value = oValue; return newOption; } function clearSelect(theSelect){ for(var i = 0;i <= theSelect.options.length+1;i++) { theSelect.remove(); } } function onSelect(theSelect){ var nextSelect = document.getElementById('ExtraField_2'); clearSelect(nextSelect); var selected = theSelect.options[theSelect.selectedIndex]; if(selected.value == 1){ nextSelect.add(createOption('test2','2')); nextSelect.add(createOption('test3','3')); nextSelect.add(createOption('test4','4')); } if(selected.value == 2){ nextSelect.add(createOption('test5','5')); nextSelect.add(createOption('test6','6')); nextSelect.add(createOption('test7','7')); } } Copywith html:<selectid="ExtraField_1"name="ExtraField_1"onchange="javascript: onSelect(this);" ><optionvalue="0">Select a test..</option><optionvalue="1">test1</option><optionvalue="2">test2</option><optionvalue="3">test3</option><optionvalue="4">test4</option><optionvalue="5">test5</option><optionvalue="6">test6</option><optionvalue="7">test7</option><optionvalue="8">test8</option><optionvalue="9">test9</option><optionvalue="10">test10</option><optionvalue="11">test11</option><optionvalue="12">test12</option></select><selectid="ExtraField_2"name="ExtraField_2"><optionvalue="0">Select from the left</option></select> Copyas you can see it still does what you expect but you are not hiding options. http://jsfiddle.net/upKzW/13/Solution 2: $('#ExtraField_1').change(function() { $('#ExtraField_2').val(this.value); Copy});http://jsfiddle.net/judearasu/rF8G6/ Share Post a Comment for "Hide/show The Selected Option On A Secondary Select" Top Question How Do I Use The Deezer Api? I'm trying to use the deezer api to look up artists and… Is It Possible To Get Clipboard Data From A User With Either Php Or Javascript? Essentially, if a user visits the page, and they press ctrl… Can't Get Text Highlighted When Multiple Tags Are Present So I am trying to highlight text by selecting it and clicki… 'Pulsing' A Border In Javascript/JQuery I am in the process of applying validation on a web form -… JQuery "find" Method Alternative $('.wrapper a').find('a'); //return empty o… Jquery Malipulate A Subarray, And Return A New Array Using Each/map I have this string in the form of ItemName1:Rate1:Tax1_Item… Tell If An Line Is Hidden I have a chart that has multiple lines, each of which is pl… Hoisting In Javascript I asked a question before and somebody give me a guide and … How To Add Transition When Changing Img Src On Hover? So I'm trying to add a different image when the origina… How To Add Input Fields Dynamically In Angular 6 I need to add input fields to array of objects and a map wh… December 2024 (1) November 2024 (38) October 2024 (62) September 2024 (23) August 2024 (372) July 2024 (342) June 2024 (701) May 2024 (1292) April 2024 (787) March 2024 (1525) February 2024 (1713) January 2024 (1336) December 2023 (1329) November 2023 (410) October 2023 (580) September 2023 (306) August 2023 (356) July 2023 (281) June 2023 (368) May 2023 (199) April 2023 (130) March 2023 (140) February 2023 (157) January 2023 (279) December 2022 (130) November 2022 (233) October 2022 (171) September 2022 (168) August 2022 (495) July 2022 (300) June 2022 (314) Menu Halaman Statis Beranda © 2022 - JavaScript Passion