Javascript instanceof errors

4 posts, 0 answers
  1. Andrew
    Andrew avatar
    104 posts
    Member since:
    May 2008

    Posted 22 Apr 2009 Link to this post

    Hi, I have a configuration generated form that can contain controls of types RadTextBox, RadNumericTextBox, RadComboBox and RadDateInput. This form, generated at runtime, can contain 0 to X number of each control. I have a function (below) that I use to get all the controls of those types that match a certain ID naming scheme.

                function GetAllControls(filter)  
                { 
                    var allLookupControls = [];  
                    var allRadControls = $telerik.radControls;                 
                      
                    for (var i = 0; i < allRadControls.length; i++)  
                    {  
                        var element = allRadControls[i];  
                        if (element instanceof Telerik.Web.UI.RadNumericTextBox || element instanceof Telerik.Web.UI.RadTextBox || 
                            element instanceof Telerik.Web.UI.RadDateInput || element instanceof Telerik.Web.UI.RadComboBox)  
                        {  
                            if(element.get_id().match(filter))  
                            { 
                                Array.add(allLookupControls, element);  
                            } 
                        }  
                    }  
                    return allLookupControls;  
                } 

    The function executes fine and returns the expected results as long as at least ONE control of each type exists on the page. If, for example, there are no RadDateInputs defined on the page however, I get a "Function Expected" javascript error. Using a javascript debugger I've been able to determine that the error occurs when trying to exeucte the "instanceof" function on the the DateInput type. Unfortunately, instanceof is the check I use to see if there are instance of the RadDateInput control so I can think of no way around this error.

    Is there any help you can offer with this issue?

    Thanks.
  2. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 23 Apr 2009 Link to this post

    Hi,

    this works for me:
    function GetAllControls(filter) {  
        var allLookupControls = [];  
        var allRadControls;  
        try {  
            allRadControls = $telerik.radControls;  
        }  
        catch (x) {  
            return;  
        }  
     
        for (var i = 0; i < allRadControls.length; i++) {  
            var element = allRadControls[i];  
            if (TrySafe(element, Telerik.Web.UI.RadNumericTextBox) || TrySafe(element, Telerik.Web.UI.RadTextBox) ||  
                    TrySafe(element, Telerik.Web.UI.RadDateInput) || TrySafe(element, Telerik.Web.UI.RadComboBox)) {  
                if (element.get_id().match(filter)) {  
                    Array.add(allLookupControls, element);  
                }  
            }  
        }  
        return allLookupControls;  
    }  
    function TrySafe(element, kindOf) {  
        try {  
            if (element instanceof kindOf) {  
                return true;  
            }  
            return false;  
        }  
        catch (x) {  
            return false;  
        }  
    }  
     
    The first try-chatch is there for the possibility that NO (not a single one) Rad controls exist on the page.

    HTH

    Manfred
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 23 Apr 2009 Link to this post

    Follow up:
    This guide http://docstore.mik.ua/orelly/webprog/jscript/ch05_05.htm at 5.5.3 (last sentence) makes clear why the error occurs I guess :)
  5. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 24 Apr 2009 Link to this post

    Hello ManniAT,

    Thank you for the comments and the code snippets you posted in this thread - they can be useful for other community members. I updated your Telerik points for the feedback.

    Kind regards,

    Sebastian
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top