If I have the following select:
<select id="multi_select" multiple>
<option id="1" value="one">One</option>
<option id="2" value="two">Two</option>
<option id="3" value="three">Three</option>
<option id="4" value="four">Four</option>
<option id="5" value="five">Five</option>
</select>
How can I get the id of the clicked option element?
If I use this:
$("#multi_select").on('change', function () {
let id = this.options[this.selectedIndex].id;
});
It doesn't work, because it returns the top most id
.
In my example, if I click option One and then I shift-click option Two (select multiple), the id
would be 1
because it's the top most selected option, but I need only the id
of the option that was clicked on
Edit Added snippet
$("#multi_select").on('change', function() {
let id = this.options[this.selectedIndex].id;
console.log(id);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="multi_select" multiple>
<option id="1" value="one">One</option>
<option id="2" value="two">Two</option>
<option id="3" value="three">Three</option>
<option id="4" value="four">Four</option>
<option id="5" value="five">Five</option>
</select>