Get All Options From Select Box (selected And Non Selected)
Using this code: $('#select-from').each(function() { alert($(this).val()); });
Solution 1:
Try below
get all values
var valuesArray = $("#select-from option").map(function(){
return this.value;
}).get();
get selected and unselected values in different array.
var values = {
selected: [],
unselected:[]
};
$("#select-from option").each(function(){
values[this.selected ? 'selected' : 'unselected'].push(this.value);
});
Thanks,
Siva
Solution 2:
This is the code you need:
$('#select-from option').each(function(){
alert($(this).val());
});
Take a look at this jsfiddle
Solution 3:
$("#selectId > option").each(function() {
alert(this.text + ' ' + this.value);
});
this.text
will give the text
this.value
will give the value
Solution 4:
My version :)
$("#select-from option:selected").each(function (i, x) {
alert($(x).val() + " selected");
});
$("#select-from option:not(:selected)").each(function (i, x) {
alert($(x).val() + " not selected");
});
Solution 5:
$('#select-from option').each(function() {
console.log($(this).text());
});
This will return all values.
If you want to highlight the selected you could do an if statement
to check if selected and then merge somthing to the output text with a +
.
Post a Comment for "Get All Options From Select Box (selected And Non Selected)"