How To Call Onchange Event When Dropdown Values Are Same February 28, 2024 Post a Comment I am using Jquery chosen plugin and it's working fine. I have used this plugin in my one of the module. My dropdown values are something like that: Solution 1: I saw your implementation and it is working fine in code pen here is the link no need to change anything<select id="itemcode" onchange="get_data()"> <optionvalue="1">ITEM001</option><optionvalue="2">ITEM002</option><optionvalue="1">ITEM001</option><optionvalue="3">ITEM003</option> </select> var get_data =function(){ alert("saas") } Copyhttp://codepen.io/vkvicky-vasudev/pen/dXXVzNSolution 2: Try this $('#itemcode').click(function() { console.log($(this).val()); });Copy<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><selectid="itemcode"><optionvalue="1">ITEM001-A</option><optionvalue="2">ITEM002</option><optionvalue="1">ITEM001-B</option><optionvalue="3">ITEM003</option></select>CopySolution 3: Edit: This doesn't work. Sorry!You could add a data attribute that differs for each element, for example:<selectid="itemcode"onchange="get_data()"><optionvalue="1"data-id="1">ITEM001</option><optionvalue="2"data-id="2">ITEM002</option><optionvalue="1"data-id="3">ITEM001</option><optionvalue="3"data-id="4">ITEM003</option></select>CopyIf you're using Rails or another framework to generate the <option> tags, it should be easy to add an incremental id to each element.Solution 4: There is no way to fire get_data() with your current data. The solution below is more of a hack. When you populate the options, prepend the value with something unique. Eg.<selectid="itemcode"onchange="get_data()"><optionvalue="1_1">ITEM001</option><optionvalue="2_2">ITEM002</option><optionvalue="3_1">ITEM001</option><optionvalue="4_3">ITEM003</option></select>CopyThus your get_data() method will be called everytime. And in your get_data() method, split the value using underscore _ and you can get the actual value there.functionget_data(){ var actualValue=$(this).val().split("_")[1]; //do other processing ... } CopyYou can use other characters like $, or anything you like, instead of _ Solution 5: Ideally you want to change the data coming from the backend so that you don't get duplicate data. However if this is not possible, another approach would be to sanitise the data before putting it in the select. E.ghttps://jsfiddle.net/vuks2bpt/var dataFromBackend = [ {key:1, value: "ITEM0001" }, {key:2, value: "ITEM0002" }, {key:1, value: "ITEM0001" }, {key:3, value: "ITEM0003" } ]; functionremoveDuplicates(array){ var o = {}; array.forEach(function(item){ o[item.key] = item.value; }); return o; } functionget_data(){ console.log('get_data'); } var sanitised = removeDuplicates(dataFromBackend); var select = document.createElement('select'); select.id = "itemcode"; select.addEventListener('change', get_data); Object.keys(sanitised).forEach(function(key){ var option = document.createElement('option'); option.value = key; option.textContent = sanitised[key]; select.appendChild(option); }) document.getElementById('container').appendChild(select); Copy Share Post a Comment for "How To Call Onchange Event When Dropdown Values Are Same" Top Question How To Style A Programatically Add Element In Angular Could I please ask for help with the following (pictures ar… Mobx Observable Array Not Updated I am declaring a observable array in the following way in r… How To Specify Color Space For Canvas In JavaScript? What is the default color space used in JS canvas? How can … How Can I Force Download With Html And/or Javascript? So I have a this code: My Song How can I make it so t… Javascript Math Different To What I Thought It Should Be Just wondering if someone knows what is wrong with my javas… Rxjs - Consume API Output And Re-query When Cache Is Empty I'm trying to implement a version of this intro to RxJS… How Can I Remove Data Which Is Stored In Local Storage? If I console.log(localStorage.getItem('cartCache'))… How Can I Detect CSS Variable Support With JavaScript? The latest version of Firefox has support for CSS Variables… How To Replace Text With Jquery? Is it possible I can replace my name with 'anonymous… Trying To Make Navigation Bar For Tablet Site I'm designing a navigation bar for a tablet website. Th… December 2024 (1) November 2024 (38) October 2024 (65) September 2024 (22) August 2024 (220) July 2024 (194) June 2024 (444) May 2024 (702) April 2024 (443) March 2024 (883) February 2024 (912) January 2024 (833) December 2023 (897) November 2023 (392) October 2023 (554) September 2023 (306) August 2023 (334) July 2023 (276) June 2023 (399) May 2023 (229) April 2023 (126) March 2023 (149) February 2023 (174) January 2023 (288) December 2022 (130) November 2022 (223) October 2022 (173) September 2022 (172) August 2022 (494) July 2022 (309) June 2022 (324) Menu Halaman Statis Beranda © 2022 - JavaScript General