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?
    – Simon
    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"
1
  • Brilliant, its good to see what was wrong rather than just copy and paste the solution. I'd accept it again if I could!
    – Simon
    Commented Jul 26, 2012 at 13:17
0
$("#select[id$='ddl_StateOfOption'] :selected").text();

try this one.

1
  • Thanks, this also give an error - "unrecognized expression". @nnnnnn answer works.
    – Simon
    Commented Jul 26, 2012 at 12:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.