there's DRY and there's verbosity to make your code easier to understand. If a law is new but its interpretation is vague, can the courts directly ask the drafters the intent and official interpretation of their law? How about using a custom jQuery event with an angular watch type interface; I know this is an old thread, but thought I may add on a little extra. Pure vanilla. The idea is to iterate over all dropdown options using the forof statement and collect values of all the <option> elements having the selected attribute. Additional Info: jQuery 1.10.2 using the syntax $('select').on('change', function(){ /* do seomthing */ }); if somebody also need to get the text of the option, here is it $(this).find('option:selected').text(); The open-source game engine youve been waiting for: Godot (Ep. How can I remove a specific item from an array in JavaScript? Is there a proper earth ground point in this switch box? In JavaScript, using the addEventListener() method: Call a function when a user changes the Then in your script you could access as any other input: Combine the focus event with the change event to achieve what you want: This will give you the last value which is selected. What is the purpose of this D-shaped ring at the base of the tongue on my hiking boots? Is there a proper earth ground point in this switch box? Story Identification: Nanomachines Building Cities. How can the mass of an unstable composite particle become complex? For example, using the following select box: If you wanted to get the string "Mr" if the first option was selected (instead of just "1") you would do that in the following way: Just in case someone is looking for a React solution without having to download addition dependancies you could write: Make sure to bind the changed() function in the constructor like: this code once i write for just explain onChange event of select you can save this code as html and see output it works.and easy to understand for you. So, I'm adding the getElementsByName() solution just for new developers to see too. You should look up the differences and make sure you are using the appropriate one. The value -1 indicates that no element is selected. Still useful - thank you for writing out the variations / language! It adds a current value to data, so the code can control when a variable set to the current value is changed. To get the selected option value first, we need to access the <select> element by using its id attribute then it has a value property which is holding the data you are selected. By attaching the change listener to the body, you can pretty much be sure this will fire after specific listeners for the selects, otherwise the value of 'data-previous' will be overwritten before you can even read it. This also standardizes the way of getting a value from form elements. How do I check whether a checkbox is checked in jQuery? The value property allows us to read or set the value of the select element. Why was the nose gear of Concorde located so far aft? How do I return the response from an asynchronous call? I'm usually doing this with the following approach (it is an easier way and works with every browser as far as I know): In 2015, in Firefox, the following also works. I cannot use the above approach beacuse i am having multiple select boxes in that same page with whom i have to deal. Set your value on some where hidden filed or local storage and get previous value from them. rev2023.3.1.43269. Let's say you have a function and you create a new select: But this does not really allow you to specify that argument passed in, so instead you may do this: And you are able to set the parameter. You can grab the value in one line: I wonder that everyone has posted about value and text option to get from