How can I combine this two selectors:
$("td.listas select:last option:selected")
This doesn’t work!
What I’m trying to achieve is:
- Get the select input from a td with the class listas
- Get the option that is selected
Notes:
- In this td, I have two select inputs.
Code working:
var selectprimeiro = $('td.listas select:first');
var selectsegundo = $('td.listas select:last option');
selectsegundo.prop("selected",true);
var selected = $("td.listas select:last option:selected");
selected.each(function(){
selectprimeiro.append("<option value='"+$(this).val()+"'>"+$(this).text()+"</option>");
selected.remove();
});
I think you want
First you get all of the selected options, then you take the last of them. Gratuitous live example | source
You might be tempted to do
option:last:selected, but that won’t work (unless the last option in the list happens to be selected), because theoptionwould have to be both:lastand:selected.Update:
Re your edit:
If they are not multi-select
selectelements, to get the selectedoptionelement:If they are multi-select, combine the two techniques above: