$find in Javascript returns null (on production server)

Hi

I have the following:

var ADcombo = $find("<%= ddlDirective.ClientID%>");

On my DevPC running IE11 & Chrome it works OK, but when I copy to production the functions don't work, I have traced it to the $find retuning a null.

The Prod server also has IE11 & Chrome, and the ddl is a Telerik RadComboBox.

Anyone any ideas?

Andy
LVL 3
Andy GreenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,
Isn't there a Hash missing?
var ADcombo = $find("#<%= ddlDirective.ClientID%>");

Open in new window

0
Rainer JeschorCommented:
Sorry, my answer is wrong. I misread the Code and assumed jQuery find, not the Ajax shorthand function.
0
Duy PhamFreelance IT ConsultantCommented:
Could you try to post the code of your ASPX page here? Or explain about the code section where you get that error and when it happens?

My best guess is that you are trying to execute the code on document ready, and maybe by that time, your page is not fully rendered/loaded.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Andy GreenAuthor Commented:
This is the control markup, and is loaded from code behind:

 <telerik:RadComboBox ID="ddlDirective" Runat="server" Width="260px"  AppendDataBoundItems="true"  CausesValidation="false" telerik:RadComboBox>

This is the javascript:
//Remove D & T
var ADcombo = $find("<%= ddlDirective.ClientID%>");
var ADitem = ADcombo.findItemByText("D (Awaiting Details)");
ADitem.disable();

What is failing is the disable() of the node because the ADCombo is null

If its trying to run before the page has loaded, why would it work on my development PC, but not on Live

Andy
0
Duy PhamFreelance IT ConsultantCommented:
If its trying to run before the page has loaded, why would it work on my development PC, but not on Live
I actually got this issue quite often when there is large view state created on page (due to many heavy controls such as grids, large data, etc.) and when registering client scripts on page_load from server side. So my 2 cents here is that we'd better be sure that control is ready at runtime (checking for null or undefined).

What is failing is the disable() of the node because the ADCombo is null
What is exactly null:  ADcombo or ADitem?

You didn't post the full code of your page, and the information about how data is bound to your RadComboBox and when your failed script is called, so I suggest you do some some checks on production:
Check the value of <%=ddlDirective.ClientID %>, make sure it's the same as the one generated after page is rendered into HTML (View Source or using Developer Tools to inspect the element)
Make sure that your RadComboBox contains data
Make sure that your RadComboBox contains item with text is exactly the same as "D (Awaiting Details)". Sometimes an extra space in the text could cause the issue
If your ADitem is null, not ADcombo, then it could be a problem with client object of your RadComboBox (viewstate is not completed loaded or corrupted, item dictionary is not rebuilt properly or not ready after rebinding, etc.). Try to loop get all items and loop through them to find your item to see if it could help.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Andy GreenAuthor Commented:
Thanks Duy

var ADcombo = $find("<%= ddlDirective.ClientID%>"); is null, its the $Find that is failing.

Defo have data in the control, and the function is run some 10's of seconds / minutes after the form has loaded.

This is the full function:

       function togglePathwayOptions(option) {
            //clearRadios();
            $('#rblFUP').find('input:radio').removeAttr('checked');
            $('#rblRefer2Another').find('input:radio').removeAttr('checked');

            $(".PathwayOutcome").hide();
            var elem = document.getElementById(option);
            elem.style.display = 'block';

            //Re-instate D & T
            var ADcombo = $find("<%= ddlDirective.ClientID%>");
            var ADitem = ADcombo.findItemByText("D (Awaiting Details)");
            ADitem.enable();

            //Re instate actions
            var fa = $find("ddlAction");
            fa.findItemByValue("1").set_checked(false);

            if (option == 'pnlAdd2IPWL') {
                ValidatorEnable($get('rfvAdd2IPWL_FU'), true);
                ValidatorEnable($get('rfuAdd2DCList_FU'), false);
                ValidatorEnable($get('rfvAdd2List'), false);

                //Show Tests stuff
                document.getElementById("R2T_1").style.display = 'table-row';
                document.getElementById("R2T_2").style.display = 'table-row';

                //set 6 weeks
                var ddlWeeeks1 = $find("ddlWeeks");
                var valWeeks1 = ddlWeeeks1.findItemByText("6 Weeks");
                valWeeks1.select();

                //Remove D & T
                var ADcombo = $find("<%= ddlDirective.ClientID%>");
                var ADitem = ADcombo.findItemByText("D (Awaiting Details)");
                ADitem.disable();

                return false;
            } 

        }

Open in new window


Andy
0
Andy GreenAuthor Commented:
Sorry Guys, this was my bad.

Thanks for stating the obvious Duy -

•Check the value of <%=ddlDirective.ClientID %>, make sure it's the same as the one generated after page is rendered into HTML (View Source or using Developer Tools to inspect the element)

This is a parameter and was set to visible = false on the code behind so wasn't on the page.

Doh!!!

Andy
0
Andy GreenAuthor Commented:
Thanks Duy.

It was last thing on a Friday :-)

Andy
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.