Using this code:
$('#select-from').each(function() {
alert($(this).val());
});
<select name="selectfrom" id="select-from" multiple="" size="15">
<option value="1">Porta iPhone Auto</option>
<option value="2">Leather Moto Vest</option
</select>
I get all the values from that select box that are selected, how do I get also the ones not selected?
Using this code:
$('#select-from').each(function() {
alert($(this).val());
});
<select name="selectfrom" id="select-from" multiple="" size="15">
<option value="1">Porta iPhone Auto</option>
<option value="2">Leather Moto Vest</option
</select>
I get all the values from that select box that are selected, how do I get also the ones not selected?
Share Improve this question asked May 24, 2013 at 9:39 DomingoSLDomingoSL 15.5k25 gold badges104 silver badges179 bronze badges5 Answers
Reset to default 7Try 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
This is the code you need:
$('#select-from option').each(function(){
alert($(this).val());
});
Take a look at this jsfiddle
$("#selectId > option").each(function() {
alert(this.text + ' ' + this.value);
});
this.text
will give the text
this.value
will give the value
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");
});
$('#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 +
.