notforever
notforever

Reputation: 589

check if multiple select has option selected

I have the next select:

<select name="extra_especialidad[]" multiple="multiple">

For any reason the name is comming with brackets and I can't change or add anything to the element.

I am trying to check if the select has any option selected, I have tried this without success...

$("input[name=extra_especialidad\\[\\]]").length; //Gives me 0 always
$("input[name=extra_especialidad\\[\\]]").val().length; //gives me error

Any advice?

Thanks in advance.

Upvotes: 10

Views: 21691

Answers (2)

domdomcodecode
domdomcodecode

Reputation: 2443

Try a 'begins with' selector so you don't have to worry about the brackets.

$("select[name^='extra_especialidad']").whatever()

the ^= in this case is matching names that start with extra_especialidad

Upvotes: -1

The Alpha
The Alpha

Reputation: 146191

You may try this (Example):

$("select[name='extra_especialidad[]'] option:selected").length;

Upvotes: 21

Related Questions