Reputation: 13
I'm trying to retrieve the ID from a selected option. Listing Name works but not ID
HTML
<select id="field" name="field" class="imput listing-select-field w-select">
<option value="">Select one...</option>
<option id="123" value="First">First</option>
<option id="111" value="Second">Second</option>
</select>
Jquery
$(".listing-select-field").on("change", function () {
sessionStorage.setItem("Listing Name", this.value);
sessionStorage.setItem("Listing Id", $(this).attr("id"));
});
Session storage shows only "field"
Upvotes: 1
Views: 1216
Reputation: 346
You can use $( ".listing-select-field option:selected" )
to get the selected options. Then you can use .id
on them.
Upvotes: 1
Reputation: 28611
Within the select
change
event, $(this)
will be the select
, not the selected option
- so, as confirmed, gives you field
for all options.
You need to locate the selected option:
sessionStorage.setItem("Listing Id", $(this).find("option:selected").attr("id"));
Upvotes: 1
Reputation: 23654
sessionStorage.setItem("Listing Name", this.value);
should be
sessionStorage.setItem("Listing Name", $(this).val());
Upvotes: 0