Should be simple but my mind is tired. How to get this jQuery to select an ASP.NET server control with an id like lblName. The IDs are different and coming in through the id variable.
var id = 'Name'; //an example
var $element = $("#<%= 'lbl'+ id.ClientID %>");
Instead of using the “<%=” tags I would suggest using the partial selectors in jquery. So your selector would look like this
This will enable you to select your controls and be able to move those selectors to an external js file instead of having to be on the same page as the control.
EDIT: Switched selector to “Contains”. The reason behind this is if you are not using .net 4.0 or you have not switched off the auto generated ids your rendered ids will be something like this: “cto001$Somethingsomething${specifiedId here}” (without the {}). So you can’t use the “Startswith” selector as that is trying to match on a dynamic part of the id.
You won’t be able to do the ends with as the last part of your id is also dynamic. So we are left with the “Contains” selector which will match any control that contains “lbl”. You may consider making the first part of the Id (non-dynamic part) less general to avoid returning unrelated controls.
EDIT2: If you are using .net 4.0 you can turn off the autogenerated Id thing and then you can use the nicer “StartsWith” (^=) selector as the rendered output id would be the same as you specified. You can do this by setting the ClientIDMode to “Predictable”