Link to home
Create AccountLog in
Avatar of rpkhare
rpkhareFlag for India

asked on

How to find a control located inside ItemTemplate using jQuery?

I want to know how to find a control with an ID located inside ItemTemplate of either a Repeater Control or a DataList control using jQuery.

Assume I have a control located as below:

<asp:RepeaterControl ID="rpt1" runat="server">
         <ItemTemplate>
                 <AjaxControlToolkit: CollapsiblePanelExtender ID="cpe" runat="server>
                 </AjaxControlToolkit: CollapsiblePanelExtender>
         </ItemTemplate>
</asp:RepeaterControl>

Open in new window


I want to get the ID of CollapsiblePanelExtender using jQuery. I tried:

$find("cpe")

but it returns null as it is not able to find any control with ID 'cpe'.
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

In jQuery just use:

$('#cpe')

That will select the element with an ID of cpe.
Avatar of rpkhare

ASKER

But it is not working when the control is located inside ItemTemplate.
$('#cpe') will select an element in your document with an ID of cpe, no matter where it is.

As for the ItemTemplate - jQuery isn't even aware of it - that's server-side code (ASP) and jQuery is exclusively client-side code.

If it's not selecting it, then it's likely that it doesn't exist. Any change of a link to your page.
I don't know ASP so I may be off-course here, but the fact that your code is inside a repeater, tells me that it repeats. An ID in an HTML document has to be unique, so you can only have one element with an ID of 'cpe' in any given page. If you have more than one element with that ID, then you need to sort your code out because it's wrong!

To check that, don't look at the ASP, Server-side code - load up your page, and then view the source from within your browser.
Avatar of rpkhare

ASKER

I opened the source from Browser and found IDs like:

ctl_100_CollapsiblePanelExtender

ctl_101_CollapsiblePanelExtender

etc.

Isn't there any way to get the ID as it is changing every time.
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account