This question already has an answer here:
How to use properly ng-change on angularjs dropdown element?
(1 answer)
Closed 5 years ago.
I have one code:
<select ng-options="i.id as i.os_version for i in devices"
ng-model="selected_version_os_to"
ng-change="selectVersion(i.id)">
<option value=''> Select version</option>
</select>
Why I get undefined in console when try to display value in:
ng-change="selectVersion(i.id)"?
Inside selectVersionfunction: I get i.id as undefined:
$scope.selectVersion = function(item) {
console.log(item);
}
You are trying to display the wrong variable.
As your ng-model is ng-model="selected_version_os_to", you should change your ng-change to:
ng-change="selectVersion(selected_version_os_to)"
You don't need to pass anything in selectVersion method, as ng-model already binding selected value for you.
Your code should be like:
<select ng-options="i.id as i.os_version for i in devices"
ng-model="selected_version_os_to"
ng-change="selectVersion()">
<option value=''> Select version</option>
</select>
// controller code
$scope.selectVersion = function() {
console.log($scope.selected_version_os_to);
}
Related
This question already has answers here:
Get selected text from a drop-down list (select box) using jQuery
(35 answers)
Closed 8 years ago.
I have a select code with two option. Is there a way I can use jQuery to alert me the text within the option tag of the option I have selected? Currently I have option two selected so when I use the code below it comes back as "5". I need it to come back as "Option 2".
alert (jQuery('#bw_status').val());
The code below is the example code I am using.
<select id="bw_status">
<option value="7">Option 1</option>
<option value="5">Option 2</option>
</select>
Yes, try this:
jQuery('#bw_status option:selected').text()
This question already has answers here:
Adding options to a <select> using jQuery?
(31 answers)
Closed 8 years ago.
I am trying js:
$('a[data-select="more-options"]').click(
function () {
$('select')
.append($("<option></option>")
.attr("one more additional and long option",3)
.text("3333333333"));
}
);
on HTML of
<span id="dropdown">A
<select>
<option>this is option 1</option>
<option>This is option 2</option>
</select> list<br>
</span>
<a data-select="more-options" href="#">Add long option to the select</a>
but I get no results (or error).
Check this fiddle.
$('select').append( "<option>This is option 3</option>" );
This appends the third option to select. You can add other attributes to the option.
Check this,
$('a[data-select="more-options"]').click(function () {
$('select').append("<option>3333333333</option>");
});
jsfiddle
This question already has answers here:
What is the most efficient way to sort an Html Select's Options by value, while preserving the currently selected item?
(12 answers)
Closed 8 years ago.
There is situation like this:
<select id="trollSelect">
<option value="PL">Polish Troll</option>
<option value="EN">English Troll</option>
<option value="JP">Japanese Troll</option>
[...] //more options created dynamically
</select>
Option values and Option texts are unsorted in Select. Is this possible to sort this options in JQuery or Javascript by value or text alphabetically ??
The result should be like that:
<select id="trollSelect">
<option value="EN">English Troll</option>
<option value="JP">Japanese Troll</option>
<option value="PL">Polish Troll</option>
[...] //more options created dynamically
</select>
This code should do it. I've sorted them on their values but this can also be used to sort based on the text.
$(document).ready(function() {
var opt = $("#trollSelect option").sort(function (a,b) { return a.value.toUpperCase().localeCompare(b.value.toUpperCase()) });
$("#trollSelect").append(opt);
});
Here's a fiddle for the same
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
retaining selected dropdown option on postback
I have a dropdown when user selects the option, the value is passed on to the same url as querystring refreshing the page. after the page refreshes i wanna retain the selected value so user knows what was selected. How do i do this in jquery?
<select id="hospitalDropDown" onchange="window.open(this.options[this.selectedIndex].value,'_top')">
<option value="http://mysite.com/events/Pages/default1.aspx">All Hospitals</option>
<option value="http://mysite.com/events/Pages/default1.aspx?hos=Dyer">Dyer</option>
<option value="http://mysite.com/events/Pages/default1.aspx?hos=Carmel">Carmel</option>
</select>
Basically the logic is trap the selection in some variable and pass it as selected equals true but i am not being able to do it in jquery..I don't have acccess to server side code..either
For a clean method you can set a cookie
Take a look at the following question and the replies
jQuery cookies setting select drop down value after page refresh
*But my favorite is to set the selection in the user session using ajax method.
<select id="hospitalDropDown">
<option value="">All Hospitals</option>
<option value="Dyer">Dyer</option>
<option value="Carmel">Carmel</option>
</select>
<script type="text/javascript">
$(document).ready(function() {
$('#hospitalDropDown').val('<?php echo $_GET['hos']; ?>');
$('#hospitalDropDown').change(function() {
location.href = 'http://mysite.com/events/Pages/default1.aspx?hos=' + $(this).val();
});
});
</script>
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Selecting an option by its text
Is there any way how to choose the option in selectbox by the text label of the option (not the value)?
<select id="form-mySelect" name="number">
<option value="0">Zero</option>
<option value="1">One</option>
<option value="2">Two</option>
</select>
$("#form-mySelect").val("One");
The example above doesn't work in jQuery 1.4.3. I've read somewhere, that in some lower version of jQuery selecting option by >text label< (not by the value) works.
Is there any way, how to simulate this behavior and make it functional in jQuery 1.4.3?
You can use $('#form-mySelect").html() to get the label contents. So you could loop all options and compare the html() value with one given and then select the option.