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.
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
if the javascript is on the same .aspx file, you can do:
$('<%= txtSomeData1.ClientID %>').val('Some More Data!!');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With