Rishi Reddy
Rishi Reddy

Reputation: 31

How to Get the Selected value for the select choice element

I have tried to get the value of the selected option as,var

doma=$('#domain_picker_select').find('option:selected').text();
OR
var doma=$('#domain_picker_select').val(); 

but i am getting the output as undefined, please help me to get the values for the option been selected for the select choice.

<select name="domain_picker_select"ng-options="domain.value as domain.label for domain in domains.list" id="domain_picker_select">
<option label="global" value="string:global" selected="selected">global</option>
<option label="TOP/Comm" value="string:6b16be7e6f72710f6">TOP/GBP/Comm</option>
<option label="TOP/Custom" value="string:e15e65256f9bd23">TOP/Custom</option>
</select>

Upvotes: 3

Views: 3671

Answers (3)

Robin Hossain
Robin Hossain

Reputation: 705

Can you please use this way:

$("#domain_picker_select").on('change', function(){
    var doma = $(this).val();
    console.log(doma);
})

Hope this trick will help you. Thank You :)

$("#domain_picker_select").on('change', function(){
  var doma = $(this).val();
  console.log(doma);
})
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>

<select name="domain_picker_select"ng-options="domain.value as domain.label for domain in domains.list" id="domain_picker_select">
<option label="global" value="string:global" selected="selected">global</option>
<option label="TOP/Comm" value="string:6b16be7e6f72710f6">TOP/GBP/Comm</option>
<option label="TOP/Custom" value="string:e15e65256f9bd23">TOP/Custom</option>
</select>

Upvotes: 0

GROVER.
GROVER.

Reputation: 4378

Give this a try:

$("select#domain_picker_select").change(function(){
    var doma = $(this).find(":selected").text();
    console.log(doma); // output value to console
});

-

<select name="domain_picker_select"ng-options="domain.value as domain.label for domain in domains.list" id="domain_picker_select">
    <option label="global" value="string:global" selected>global</option>
    <option label="TOP/Comm" value="string:6b16be7e6f72710f6">TOP/GBP/Comm</option>
    <option label="TOP/Custom" value="string:e15e65256f9bd23">TOP/Custom</option>
</select>

EDIT:

If you want to grab the selection on load:

$(document).ready(function(){ // on document load
    var doma = $("select#domain_picker_select").find(":selected").text();
});

Upvotes: 0

nartoan
nartoan

Reputation: 378

To get value, you use val()

$("#domain_picker_select").val();

It work good! https://codepen.io/anon/pen/ZBNxPo

Upvotes: 1

Related Questions