0

I'm trying to get the selected text from a dropdown using a selector. If I reference the dropdown directly by name it works:

$('#aBigLongASP.NETWebformsGeneratedName_ddl_StateOfOption :selected').text()

I am however trying to use a selector to select the dropdown using only the last part of the name:

$('#select[id$='ddl_StateOfOption']) :selected).text();

but I can't quite seem to get it to work. The Chrome developer tool throws the following error:

SyntaxError: Unexpected identifier

Can anyone point out where the error is?

3
  • 2
    The Stack Overflow syntax highlighter should have already pointed out one of the errors. You're nesting single quotes. Replace one set with double quotes. Commented Jul 26, 2012 at 12:52
  • Also get rid of the random closing parenthesis after the closing square bracket. Commented Jul 26, 2012 at 12:54
  • Vote to closer - care to justify? Commented Jul 26, 2012 at 13:18

2 Answers 2

5

Try this:

$('select[id$="ddl_StateOfOption"] :selected').text();

There were several problems with your code:

// $('#select[id$='ddl_StateOfOption']) :selected).text();
//    ^           ^                 ^ ^          ^
//    |           |                 | |           \
//    |           |                 |  \            missing closing '
//    |           \                 /   shouldn't have )
//    \            should be " not '
//     You were selecting elements with id "select" rather than tag "select"
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant, its good to see what was wrong rather than just copy and paste the solution. I'd accept it again if I could!
0
$("#select[id$='ddl_StateOfOption'] :selected").text();

try this one.

1 Comment

Thanks, this also give an error - "unrecognized expression". @nnnnnn answer works.

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.