How To Call Onchange Event When Dropdown Values Are Same April 17, 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 JQuery: If A Table Header Has A Class, Add Class To Table Cell Let's say I have the following html: … WebRTC Video Constraints Not Working I'm trying to get a lower resolution from the webcam na… HasClass Doesn't Work In My Js Code? I want to use hasClass in the following code, but that does… Capturing .click For URLs I am relatively new to Javascript/Ajax. When the user click… Highcharts Donutchart: Avoid Showing Duplicate Legend With Nested Charts I am trying to represent nested data using Highcharts Donut… How Do I Use Data From Vue.js Child Component Within Parent Component? I have a form component where I use a child component. I wa… Logic For The Next Button For The Questionnaire? I am beginner in AngularJS and facing some issues. I am try… Assignment To Property Of Function Parameter (no-param-reassign) I have this function and while I have this working nicely, … Payload Error In Jsonwebtoken I am making a web application using nodejs and angular cli … How To Show Website Preloader Only Once I added a preloader to my website and the preloader animati… December 2024 (1) November 2024 (36) October 2024 (71) September 2024 (24) August 2024 (366) July 2024 (341) June 2024 (707) May 2024 (1298) April 2024 (798) March 2024 (1514) February 2024 (1659) January 2024 (1321) December 2023 (1338) November 2023 (386) October 2023 (580) September 2023 (268) August 2023 (327) July 2023 (258) June 2023 (361) May 2023 (223) April 2023 (123) March 2023 (145) February 2023 (183) January 2023 (272) December 2022 (128) November 2022 (234) October 2022 (168) September 2022 (166) August 2022 (495) July 2022 (290) June 2022 (289) Menu Halaman Statis Beranda © 2022 - JavaScript Creator