How can I add a <select> option using jquery? [duplicate] - javascript

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

Related

Getting the value of unselected the dropdown item [duplicate]

This question already has answers here:
How can i get the unselected items from dropdown using jQuery? [closed]
(1 answer)
Get list of values from dropdown that are NOT selected using jquery
(2 answers)
How to get currently unselected option value when un selecting from multi list box
(2 answers)
get unselected list using jquery multi select box
(2 answers)
Closed 3 years ago.
By default, I have all the dropdown items selected. But when a user unselects one of them, I want to get the value of that unselected option.
I have tried the following option:
This is my html code:
<select class="ss-select" data-dropup-auto="false" id="ss_options" multiple="multiple" name="ss">
<option selected="selected" value="1">One</option>
<option selected="selected" value="2">Two</option>
<option selected="selected" value="3">Three</option>
<option selected="selected" value="4">Four</option>
<option selected="selected" value="5">Five</option>
</select>
This is the jquery code I tried:
$("#ss_options").change(function(){
alert($(this).val());
});
With this code, when I unselect an option (example: one), I get the values as [2, 3, 4, 5]. I am aware that I can do array subtraction to get the desired value, but is there some better of doing it?
Use :not and :selected:
alert($("#ss_options option:not(:selected)").val());

ng-change works incorrect? [duplicate]

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);
}

jQuery alert text in option from select tag [duplicate]

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()

Sort Options in HTML Select [duplicate]

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

Choose selectbox option by javascript by text [duplicate]

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.

Categories

Resources