how to use dynamically generated .Net element IDs on page in jquery or css


/ Published in: ASP
Save to your folder(s)

In the examples, below, Step1 is the id given to the element. If the element is set to runat="server", when it is rendered on the page, the id will change dynamically to something like ctl00$PlaceHolderMain$Step1. But the actual value may change. To make sure we capture the correct value to manipulate the element in javascript or CSS, use the technique below. Note that this code will need to live on the same page as the element, not in an external js or css file.


Copy this code and paste it in your HTML
  1. <%--jQuery example -->
  2.  
  3. <script>
  4. $('#<%=Step1.ClientID %>').someFunction();
  5. </script>
  6.  
  7. <%--CSS example -->
  8. <style>
  9. #<%=Step1.ClientID %> {some style...}
  10. </style>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.