I’ve got an asp.net control inside a masterpage, so I thought the following selector would get me the selected option for my drop down list, but it’s in correct.
$("#input[id$='ddlTags'] option:selected")
Can anyone shed some light?
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Sometimes with master pages, or elements in a repeater, the actual client ID of controls gets mangled something fierce. However, you can send out the client ID to the client in your JavaScript. I’m going to assume your control is named ddlTags. 🙂
That’ll give you back a jQuery object containing all the selected option tags. However, since you’re using a ddl, I’m assuming it’s a single select. If all you want is the value of the selection, you can get a bit simpler.
That gives you the value of the selected items within that element.
The <%= is shorthand for “echo this piece out to the client when rendering”. It’s a really handy piece in conjunction with jQuery.
Really, you want to use an ID-based selector like this over using something like input[id=<%= ddlTags.ClientID %>] because then jQuery can use browser-native functions to make the selection and it’ll run a good bit faster.