Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selectors for select / option:selected

Tags:

jquery

How should I understand

$("select option:selected")

in the following code ?

(taken from here)

$("select").change(function() {
   ...
   $("select option:selected").each(function () {
      ...
   });
   ...
})

Is it all selected options in all selects in the document ?

Is it somehow related to the current select, $(this) ?

like image 343
Misha Moroshko Avatar asked Apr 19 '10 07:04

Misha Moroshko


2 Answers

Yes, it will refer to all selected options in all selects. If you just want to look at the current select, you can do something like this:

$("select").change(function() {
   ...
   $(this).find("option:selected").each(function () {
      ...
   });
   ...
})
like image 81
Dean Harding Avatar answered Sep 28 '22 12:09

Dean Harding


It's selected options from whole document. You can use find to select only from $(this)

like image 33
Jarek Avatar answered Sep 28 '22 11:09

Jarek