0

This has been bothering me all day, if anyone can help I would be greatful.

if(aaid) {
    var num_one = aaid;
    var radioBox = document.getElementsByName('used_dealer_before');
    for (i = 0; i < radioBox.length; i++) {
        if(num_one) {
            radioBox[i].checked = true; //this auto checks the box
        }
    }
    radioBox.onchange=function(){
        document.getElementById("previous_dealer").value=num_one; //this auto selects the     value from the dropdown
    }
}

The function above auto checks a radiobox if a variable (aaid) is present, once this box is checked there is another function in another script file (to which I do not have access; long story) that presents a dropdown menu. The next part of MY script auto-selects a value on the dropdown. This script works and has been tested locally. However, when I upload it to the server it does not execute the 2nd part of the script anymore. I suspect because the function already executes, when the page hasn't finished rendering the dropdown value. Therefore, can anyone explain to me how to use JavaScript to wait for the dropdown to appear, before it continues with the script?

Should I time delay the function? I cannot use jQuery, I have to use JavaScript.

2
  • 1
    Is radioBox.length > 0. on page where you have the problem? radioBox.onchange = ... does not make sense becaus radioBox is an array. Commented Oct 10, 2013 at 5:43
  • How is that other script file (which you don't have access to, long story) included in your page. Does it lazy load via document.createElement('script') ? Commented Oct 10, 2013 at 6:15

1 Answer 1

1

It sounds like you're looking to set up a callback function. This is commonly done when requesting external services using XHR/AJAX to get external data or code. For instance, when you want something from a server and want to be notified when it's ready, you might define a callback function using a query string parameter:

http://somedomain.com/resources?arg1=val1&callback=scriptready

The value given for the callback parameter would correspond to some globally accessible function:

window['scriptready'] = function(val){ console.log("got response: "+val); }

If this resource you're using provides the option of using a callback, then that's you're answer. If it doesn't, then you'll need some more convoluted method for setting up a listener based on when your box is checked or whatever.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.