Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

get selector value after selection?

I’m curious as to if it is possible to get this selector value after the user selects a value?

 <select>
   <option>test1</option>
   <option>test2</option>
 </select>

 //Idea of what i'm trying to accomplish
 select.addEventListener('select',()=>{
     console.log(select.value);
 })

I’m trying to use this value data to determine which item to load and view before you submit the form.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

First of all, you have to locate your select element in the script. To do this, you need to provide it with a specific id.

<select id="test_selector">
   <option>test1</option>
   <option>test2</option>
 </select>

Then you can use this id to access that selector in the JS. In order to get the updated value, chain it with the change event listener. Its callback receives the event parameter which contains the info you need.

 document.getElementById('test_selector').addEventListener('change', event => {
     console.log(event.target.value);
 });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading