Jeff,

>I apologize in advance if this is in the docs, but I probably won't be
>able to access the jQuery site for a while now that the ip has
>changed, due to proxy issues.
>
>I want to limit a user from entering more than a specified number of
>options, and can't find the correct syntax (or maybe I can't do
>this?).  Why aren't these equivalent?
>
>var num = document.getElementById('mySelect').length // returns 5
>
>var num = $("#mySelect").length // returns 1; doesn't change so I know
>this isn't correct.

There are several solutions to this problem. In order to get to the actual
DOM object, you'd want to use the following syntax:

var num = $("#mySelect")[0].length // jQuery returns an array of elements

-- or --

var num = $("#mySelect").get(0).length;

These are both the same thing, I prefer the array notation, but some people
may prefer to use the get() method.

You could also do:

var num = $("#mySelect option:selected").length

The above example would use a CSS selector to get all the option tags in the
#mySelect element that are selected.

Personally, I'd use $("#mySelect")[0].length, since it uses the actual DOM
property for the <select /> element. This should be faster than parsing out
the option tags that are selected.

However, the $("#mySelect option:selected") would allow you do other things
with those elements (such as unselect them, change their styles, etc.)

-Dan

Reply via email to