How To Call Onchange Event When Dropdown Values Are Same November 27, 2022 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()"> <option value="1">ITEM001</option> <option value="2">ITEM002</option> <option value="1">ITEM001</option> <option value="3">ITEM003</option> </select> var get_data =function(){ alert("saas") } Copy http://codepen.io/vkvicky-vasudev/pen/dXXVzN Solution 2: Try this $('#itemcode').click(function() { console.log($(this).val()); });Copy <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="itemcode"> <option value="1">ITEM001-A</option> <option value="2">ITEM002</option> <option value="1">ITEM001-B</option> <option value="3">ITEM003</option> </select>Copy Solution 3: Edit: This doesn't work. Sorry! You could add a data attribute that differs for each element, for example: <select id="itemcode" onchange="get_data()"> <option value="1" data-id="1">ITEM001</option> <option value="2" data-id="2">ITEM002</option> <option value="1" data-id="3">ITEM001</option> <option value="3" data-id="4">ITEM003</option> </select> Copy If 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. <select id="itemcode" onchange="get_data()"> <option value="1_1">ITEM001</option> <option value="2_2">ITEM002</option> <option value="3_1">ITEM001</option> <option value="4_3">ITEM003</option> </select> Copy Thus 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. function get_data(){ var actualValue=$(this).val().split("_")[1]; //do other processing ... } Copy You 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.g https://jsfiddle.net/vuks2bpt/ var dataFromBackend = [ {key:1, value: "ITEM0001" }, {key:2, value: "ITEM0002" }, {key:1, value: "ITEM0001" }, {key:3, value: "ITEM0003" } ]; function removeDuplicates(array){ var o = {}; array.forEach(function(item){ o[item.key] = item.value; }); return o; } function get_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 Spell Email Address For Forums How can I make the code of the mail to spell it and undete… Select All Checkbox In AngularJS I have a select all checkbox and list check-box like this. … Update Panel With ClientScriptManager code protected void a_Click(object sender, EventArgs … Change Input Value With Javascript The title seems easy, but I need help. I have a form with a… Javascript Select Change Field Price With Discount Onchange I have an html form with Sub-Total, Discount and Total. The… Javascript Error 'has No Method Push' I'm getting the error: object 0 has no method 'push… Hoisting In Javascript I asked a question before and somebody give me a guide and … Passport-google-oauth Callback Not Working When Used In Web Service I Have used Passport-Google-OAuth in Node.js web service pr… Binding Array Of Object To Kendo Grid Popup Multiselect I'm trying to bind an array of id-value pairs to a kend… React Native Link Using Expo? How can I use react-native link or How can I link a third p… November 2024 (35) October 2024 (54) September 2024 (19) August 2024 (198) July 2024 (183) June 2024 (399) May 2024 (680) April 2024 (428) March 2024 (764) February 2024 (891) January 2024 (789) December 2023 (815) November 2023 (346) October 2023 (524) September 2023 (279) August 2023 (339) July 2023 (281) June 2023 (344) May 2023 (209) April 2023 (123) March 2023 (145) February 2023 (175) January 2023 (279) December 2022 (135) November 2022 (234) October 2022 (176) September 2022 (165) August 2022 (494) July 2022 (294) June 2022 (309) Menu Halaman Statis Beranda © 2022 - JavaScript Exercises