0

What instruction should I use so this function is executed once the page loads instead of on click? I tried:

window.onload=function () {
("#geocomplete").trigger("geocode");

but the code above didn't work.

<script>
    $(function () {
        $("#variables").variables({
            map: ".map_canvas",
            details: "form",
            types: ["calculations", "grades"]
        });

        $("#submit_button").click(function () {

            $("#variables").trigger("calculations");
        });
    });
</script>
4
  • 4
    What's wrong with putting it inside that last code block?
    – David G
    Commented Feb 25, 2013 at 1:23
  • What is the variables() plugin that you're using? Commented Feb 25, 2013 at 1:29
  • What do you want to trigger exactly? Commented Feb 25, 2013 at 1:30
  • You second code block (with the <script> tag) should fire when the document is done loading due to you having $(function() {...}), as per jQuery's .ready() docs (api.jquery.com/ready). Assuming $ is jQuery at least...
    – ajp15243
    Commented Feb 25, 2013 at 1:32

1 Answer 1

2

window.onload happens before jQuery's event $(document).ready() (which as you're probably aware, you've set up using the shorthand version, $(function() {...})).

So you're trying to trigger an event that hasn't been set up yet.

As @David pointed out, you could just put the trigger code at the end of your existing code inside $(function() {...}).

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.