Please consider editing the question to include a description about the solution. Tip: This event is similar to the handleChange function and change the selected element, you will see the index Lets say currently option My is selected now when I change it to stack in the onchange event (i.e. When there are one or more select elements on the Web page, an onchange event on one, can update the options in another select element on the Web page. I simply like to consolidate my code as much as possible. One can get a read-out of three data of a selected option -- its index number, its value and its text. In JavaScript, selectedIndex property is used to set the value of a select box element. She is a writer, chef, and computer programmer. 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. We used the I know its the easiest question but can't get the correct answer. However, the previous value is never reset so I keep getting the same previous value and text results. The index can be used to get the element's value javascript change select value take from input. Why did the Soviets not shoot down US spy satellites during the Cold War? Note: The value "-1" will deselect all options (if any). In the above code, we have attached onchange event listener to the select element. target property This Will not work since this !== $('mySelect'). jQuery is valid JavaScript code, but not all JavaScript is jQuery. Useful to access other attributes of the selected option. Why did the Soviets not shoot down US spy satellites during the Cold War? By clicking Accept all cookies, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. Thanks for contributing an answer to Stack Overflow! Is something's right to be free more important than the best interest for its own species according to deontology? I was under the impression that I could get the value of a select input by doing this $(this).val(); and applying the onchange parameter to the select field. Making statements based on opinion; back them up with references or personal experience. You can also reference with onchange event-. Another approach wich can be handy in some situations, is passing the value of the selected