Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use JQuery, select element by ID and ASP.NET without putting ctl00_ everywhere in the code

Tags:

jquery

asp.net

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.

like image 951
digiguru Avatar asked Aug 05 '09 10:08

digiguru


People also ask

How do I target a specific element in jQuery?

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.

How can I select an element by name with jQuery?

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.

Which method of jQuery is used to get the contents of all the matched 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.


1 Answers

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");
like image 91
d4nt Avatar answered Oct 05 '22 22:10

d4nt