Macko
Macko

Reputation: 5

How to get the # of options with the attr "selected" in a drop down using jQuery?

This isn't working for me:

    $('select').each(function() {  

        alert($(this).find('option').attr('selected').length);

    }

actually what I really need is to just detect if the present select has any preselected options.

Upvotes: 0

Views: 69

Answers (1)

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262939

You can use the :selected selector:

alert($(this).find("option:selected").length);

Or, alternately, using this as the selector's context:

alert($("option:selected", this).length);

Upvotes: 3

Related Questions