I need to get "ID" of option instead of value.
Example.
<select id="industry">
<option id ="1" value="11">one</option>
<option id ="2" value="22">Two</option>
</select>
I know I can get values using the following
$("#industry").on("change",function(){
var GetValue=$("#industry").val();
});
Is the any option to get Id
CodePudding user response:
You can use var GetValue=$("#industry option:selected").attr("id");
to do it.
Explaination:
#industry
: Will look for element with idindustry
#industry option
: Will look for options underindustry
dropdownoption:selected
: Will look for selected option.:selected
is an attribute selector that looks for selected attribute in element
Once you have element, you can retrieve the id
property from it.
$("#industry").on("change",function(){
var GetValue=$("#industry option:selected").attr("id");
alert(GetValue)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="industry">
<option id ="1" value="11">one</option>
<option id ="2" value="22">Two</option>
</select>