[SOLVED] Selecting by ID attribute using JQuery in ASP.NET

Issue

I’ve just started using JQuery in VS 2008, and so far I like it! But, I’m confused about how I should be using JQuery in order to select asp.net controls on a webpage.

For example, I have the following code (just a mock-up):

<asp:textbox id="txtSomeData1" runat="server" text="Some Data!!"></textbox>

Now, if I want to use JQuery to select the textbox and change it’s text to “Some More Data!!”, then I would have to do something like:

$('input#ctl00_ContentPlaceHolder1_txtSomeData1').val('Some More Data!!');

Which, quite frankly, is annoying because I don’t want to mess with having to figure out what the id of the control is after it’s rendered to the webpage (ctl00_ContextPlaceHolder… blah blah blah).

Is there a way that I can select the textbox without having to use the id of it? Also, I know that you can select by class name, but that doesn’t help much if the control that you’re selecting doesn’t have a class.

Am I just missing something here?

JUST TO REITERATE: I do not want to use a class to select the input tag!! I would like to use the id “txtSomeData1” and not the long id that is rendered to the webpage.

Solution

What you want to do is either:

$("input[id$='_txtSomeData1']").val().....

or you could add a class or custom attribute to the textbox that you can select on

Answered By – Jaime

Answer Checked By – Marie Seifert (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *