views:

90

answers:

3

Is there a JQuery equivalent of ASP.Net Ajax's $find() function?

$() != $find()
+1  A: 

There is not a 1to1 equivalent but what you want is $('selector')

Check out the docs on the different selectors

$find('MyComponent') would be $('#MyComponent')

$find('MyComponent',div) would be $(div).find('#MyComponent')
PetersenDidIt
+1  A: 

If you want to find an element by its ASP.NET code ID rather than the generated ClientID (ctl00_RealId) then you can use this function. It just looks for elements that have an ID that ends with _{the real ID here}:

var $$ = function (id, context) {
    var $ = (jQuery) ? jQuery : return ;
    var el = $("#" + id, context);
      if (el.length < 1)
        el = $("[id$=_" + id + "]", context);
    return el;
}

For example, say your ID in your code is pnlSuccess, say a panel:

<asp:Panel ID="pnlSuccess" runat="server"></asp:Panel>

But in the rendered code it comes out as: ctl00_content_ctl00_pnlSuccess

calling $$("pnlSuccess") will find that rendered panel.

Mark Ursino
this could be very unreliable in UserControl situations where a lot of controls could end with the same ID but have a different prefix
Marek Karbarz
In that case, using the `context` would make more sense.
Mark Ursino
yup. i know that. actually i've been using $("input[name*=pnlSuccess]) to find the control. What I actually want is the object that $find() returns. it seems that the object that $find() return in ASP.Net Ajax differs from the object that $() in Jquery returns. I definitely noticed this when using Telerik Controls.
pauldomag
A: 

I'd just do the following, no muss, no fuss, straight to the point.

$('#' + <%=myControl.ClientID%>)
blesh
That assumes you're on the page that your rendered code is on, not in an external JavaScript file.
Mark Ursino
yup. i know that. actually i've been using $("input[name*=pnlSuccess]) to find the control. What I actually want is the object that $find() returns. it seems that the object that $find() return in ASP.Net Ajax differs from the object that $() in Jquery returns. I definitely noticed this when using Telerik Controls.
pauldomag
That's because $find() just returns a DOM Element, and $() returns a jQuery object wrapping an array of DOM Elements, nonetheless, $() is just as useful, probably more so.
blesh
actually, $find() doesn't return a DOM Element. its the $get() function that returns a DOM element...
pauldomag