I\'m trying to find the best way to deal with the way that ASP.NET prepends a value to any element that is created with runat=\"server\"
, without having to reso
The right answer is as follow
var txt1 = $('#<%= txt1.ClientID %>');
$(txt1).click(function () {
alert('hi');
});
her I create a new variable on the javascript which is reference to the control based on its rendered client ID
If you are using .Net framework 4.0 or higher ,Use the ClientIDMode property of the Control
You may use Static as the ClientIDModeEnumeration
The ClientID value is set to the value of the ID property. If the control is a naming container, the control is used as the top of the hierarchy of naming containers for any controls that it contains.
Why not just use ClientIDMode=Static? Set it in the Web.config so that your client IDs will be as-written throughout the site.
You can use the jQuery ends with selector:
$('input[id$="myServerId"]')
But remember, that kind of selector is a lot slower than selecting directly with the id with <%= id.ClientID %>
.
http://jsperf.com/id-vs-ends-with