Get selected value in dropdown list using JavaScript?
<select id="ViewBy">
<option value="1">Subjet1</option>
<option value="2" selected="selected">Subject2</option>
<option value="3">Subject3</option>
</select>
Running this code:
var e = document.getElementById("ViewBy");
var strUser = e.options[e.selectedIndex].value;
Would make strUser be 2.
If what you actually want is Subject2, then do this:
var e = document.getElementById("ViewBy");
var strUser = e.options[e.selectedIndex].text;
Which would make strUser be Subject2
--------------------------------------------------------------------------------------------
jQuery:
$("#elementId :selected").text() //the text content of the selected option
$("#elementId").val() //the value of the selected option
------------------------------------------------------------------------------------------------
AngularJS
// html
<select ng-model="selectItem" ng-options="item as item.text for item in items">
</select>
<p>Text: {{selectItem.text}}</p>
<p>Value: {{selectItem.value}}</p>
// javascript
$scope.items = [{
value: 'item_1_id',
text: 'Item 1'
}, {
value: 'item_2_id',
text: 'Item 2'
}];
<select id="ViewBy">
<option value="1">Subjet1</option>
<option value="2" selected="selected">Subject2</option>
<option value="3">Subject3</option>
</select>
Running this code:
var e = document.getElementById("ViewBy");
var strUser = e.options[e.selectedIndex].value;
Would make strUser be 2.
If what you actually want is Subject2, then do this:
var e = document.getElementById("ViewBy");
var strUser = e.options[e.selectedIndex].text;
Which would make strUser be Subject2
--------------------------------------------------------------------------------------------
jQuery:
$("#elementId :selected").text() //the text content of the selected option
$("#elementId").val() //the value of the selected option
------------------------------------------------------------------------------------------------
AngularJS
// html
<select ng-model="selectItem" ng-options="item as item.text for item in items">
</select>
<p>Text: {{selectItem.text}}</p>
<p>Value: {{selectItem.value}}</p>
// javascript
$scope.items = [{
value: 'item_1_id',
text: 'Item 1'
}, {
value: 'item_2_id',
text: 'Item 2'
}];
ReplyDeleteThanks for the great information in your blog PHP
nice blog. PHP academic projects in Thrissur
ReplyDeleteExcellent post, it was really useful to me. Thanks for sharing :)
ReplyDeletebest devops training in chennai
ReplyDeletebest hadoop training in chennai
best hadoop training in omr
hadoop training in sholinganallur
best java training in chennai
best python training in chennai
selenium training in chennai