I have about 600+ references of code similar to the following...
$("#ctl00_ContentMainPane_eliteUser").html
And changed the master template, only to find all the code had broken, due to the control hierarchy changing.
All javascript is included in separate files, so I cannot use code such as this...
$("#<%= eliteUser.clientID%>").html
I want to have a good solution to fixing this issue so that after fixing it once, I don't have to modify all the code if the template changes in the future.
The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.
Answer: Use the Attribute Selector You can use the CSS attribute selectors to select an HTML element by name using jQuery. The attribute selectors provide a very flexible and powerful mechanism for selecting elements.
The html() method in jQuery is used to get the contents of the first element in the set of matched elements or is used to set the HTML contents of every matched element. It returns the content of the first matched element. This function does not accept any arguments.
How about defining a JavaScript function in your master page such as:
function getContainerId() {
return "<%=ContentMainPane.ClientID %>";
}
Then in your included JavaScript files do something like this:
$("#" + getContainerId() + "_eliteUser");
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