views:

346

answers:

5

I am using a combination of jQuery and IScriptControls and I don't appear to be able to use $find in any jQuery functions.

Take the following, for example, I can use $get and $, but I cannot use $find.

    // Configure the toolbar groups
    $(document).ready(function()
        {

            // Returns the control
            var works1 = $get("ctl00_ContentPlaceHolder1_uwt_MainNavigation");
            var works2 = $("#ctl00_ContentPlaceHolder1_uwt_MainNavigation");

            // Returns null
            var broken = $find("ctl00_ContentPlaceHolder1_uwt_MainNavigation");

        }
    );

When my page loads I need to call a method that needs to get the selected tab from my MainNavigation Tab control (It's a Infragistics UltraWebTab, but I have tested with my own IScriptControls to ensure that's it's not an Infragistics issue).

The tab index can only be obtained by using $find. What's the reason I cannot use $find and how can I get the control in a $find fashion?

// Configure the toolbar groups
$(document).ready(function()
    {

        // Get the UltraWebTab Control
        var tabControl = $find("<%=uwt_MainNavigation.ClientID %>");
        var index = tabControl.get_selectedTab();
        ToolBarShowGroup(index);

    }
);

The above is what I'm trying to do, where ToolBarShowGroup calls a jQuery function to show and hide toolbars.

Also, whilst I'm hear, if someone could correct my terminology where it comes to IScript Controls.. are they 'Ajax Controls' or 'Extender Controls' or what? I've seen them referred to as all different things. The controls have the ol' MyCompany.MyControl.prototype declarations.

EDIT: The following works perfectly, but I'd rather it was inside the $(document).ready function.

// Use the Ajax Load Methods
function pageLoad()
{
    var ajaxControl= $find("<%=myControlHere.ClientID %>");
}
A: 

It seems that you are using jQuery with other libraries which also redefine the $ function. You could use the noConflict function which forces you to always use jQuery instead of $.

jQuery.noConflict();


// Configure the toolbar groups
jQuery(document).ready(function() {
    // Returns the control
    var works1 = $get("ctl00_ContentPlaceHolder1_uwt_MainNavigation");
    var works2 = jQuery("#ctl00_ContentPlaceHolder1_uwt_MainNavigation");
    // Returns null
    var broken = $find("ctl00_ContentPlaceHolder1_uwt_MainNavigation");
});

jQuery(document).ready(function() {
    // Get the UltraWebTab Control
    var tabControl = $find("<%=uwt_MainNavigation.ClientID %>");
    var index = tabControl.get_selectedTab();
    ToolBarShowGroup(index);
});
Darin Dimitrov
I am able to use $find when not inside a jQuery function. I.e. if I am just inside a normal JS function, I can use jQuery and $find, $get all within the same function. As soon as I am within a jQuery function $find becomes unfunctional. I have tried your suggestion, but it makes no difference.
GenericTypeTea
A: 

I am not sure of why you're having this problem, but why don't you just use the jQuery object instead? Like:

var mainNav = $("#ctl00_ContentPlaceHolder1_uwt_MainNavigation");

Or if you want the DOM object instead of the jQuery object, you can write as:

var mainNav = $("#ctl00_ContentPlaceHolder1_uwt_MainNavigation")[0];
Technowise
Because they do not return the same properties as the $find method. $find will return my get_ and set_ methods for the control as well as all my private members. $(control)[0] is the same as $get.
GenericTypeTea
A: 

This should work:

$(document).find("#elementid")

Or if you're calling it inside an event handler then this is even better:

$(this).find("#elementid")

I think the idea with the find function is to find descendants of a parent control. And so it doesn't seem to work if you don't specify the parent.

However, from the looks of it, you don't need to use find for what you're doing. Why not simply do this?

$("#elementid")
Steve Wortham
He is having some other library function named $find(). He is not talking about the jQuery find function here.
Technowise
Technowise, are you sure? Because I tested find just now and it doesn't work the way he has it written. I had to specify (document) for it to work.
Steve Wortham
Yes, I'm sure.The function he is trying to use is explained here:http://mattberseth.com/blog/2007/08/the_everuseful_get_and_find_as.html
Technowise
I am NOT trying to do a jQuery find. I am trying to use the Ajax Find method.
GenericTypeTea
The Ajax Find method exposes all of the Ajax properties and methods.
GenericTypeTea
You could have been more specific on that to avoid this confusion. Everyone here is not an ASP.NET Ajax developer. So for us, $find function doesn't mean ASP.NET $find function, and even Ajax Find doesn't automatically mean 'ASP.NET Ajax Find method'.
Technowise
I did mention Ajax in the last paragraph of my question and in the Edit supplement... however I will be more clear next time.
GenericTypeTea
+1  A: 

What about redefining the $find function outside of document.ready

var FIND_FUNCTION = $find;

$(document.ready) { 
   ...
   var result = FIND_FUNCTION("ctl00_ContentPlaceHolder1_uwt_MainNavigation");
}

this should fix the scoping problem you seem to be having.

Deeksy
Looks good, but it's not right. I'm starting to think that the control isn't actually created when $(document).reader is fired. Good idea though! +1
GenericTypeTea
A: 

It appears that jQuery's $(document).ready fires before the Ajax controls are constructed.

The way I got this working was to use the following JavaScript method which is fired by the Ajax framework:

function pageLoad()
{
    // $find() works in here
}

pageLoad() fires after $(document).ready, so it appears that when jQuery's function fires to say the document is ready... it isn't actually all ready?

GenericTypeTea