Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting by ID attribute using JQuery in ASP.NET

Tags:

jquery

asp.net

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.

like image 612
Jagd Avatar asked Aug 13 '09 21:08

Jagd


2 Answers

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

like image 74
Jaime Avatar answered Oct 01 '22 02:10

Jaime


if the javascript is on the same .aspx file, you can do:

$('<%= txtSomeData1.ClientID %>').val('Some More Data!!');
like image 26
BlackTigerX Avatar answered Oct 01 '22 03:10

BlackTigerX