0

I have following drop down list code, which I am trying to validate for selection. I am getting the length 0 in all cases. How can I check if an option is selected?

<select name="handpick"  size="10" multiple>
<option value="25248739">New York
<option value="25248716">California
</select>



var handpickselector = $('select[name="handpick"]');
        if ($("handpickselector option:selected").length <= 0){

            //show error message

            return false;
        }

1 Answer 1

1

Your selector is incorrect. Since it's been wrapped in "", JS is treating handpickselector as part of the string itself. Just update as follows:

if( handpickselector.find('option:selected').length == 0 )

Notice we also do an exact comparison (i.e. ==), since a jQuery collection cannot have a length < 0.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.