2

I have an html file which is correctly linked to a js file (and also the jquery library file).

<input type="button" onclick="practice();" id="intro_next_button" value="Practice"/>

Inside the js file:

$(window).load(function(){
    function practice(){
        alert("WORKS");
    }
});     

This runs if function practice is outside of the $(window).load(function(), but not when it is inside of it. There are many similar questions to this, but I have not found one that is specifically about an onclick event only not working inside of a jquery $(window).load(function().

4
  • because the function is in the block scope of onload and is not global. Why is it defined in the onload? Move it out. Commented Oct 12, 2016 at 0:08
  • Oh, I was under the impression that I should put everything in the onload function in order to make sure it works. Commented Oct 12, 2016 at 0:11
  • That would not be the case Commented Oct 12, 2016 at 0:14
  • This question is similar to: What is the scope of variables in JavaScript?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Oct 10, 2024 at 14:07

3 Answers 3

2

This doesn't work because anything defined (i.e. via keywords function or var) inside a function block applies that block as it's scope. So if you make a variable, x, inside the block you won't have the value outside there.

var x = 2;
function test() { var x = 5; }
console.log(x);

Prints 2.

These are also competing styles. Typically if you use window.onload or the "improved" jQuery $(document).ready you bind events in the body of the function.

$("#intro_next_button").click(function() { alert("WORKS"); });
Sign up to request clarification or add additional context in comments.

Comments

2

What you are doing is defining a function inside the function that is passed to load() as a parameter.

onclick is looking for practice() in the window scope.

Why you want to DEFINE the function when page is loaded? to wait until is loaded so the user can click it? If so, try this:

$(window).load(function(){
  window.practice = function(){
    alert("WORKS");
  }
});

2 Comments

How would I call the window.practice function in the onclick? I tried onclick="practice()" and onclick="window.practice()"
when you define something in window, its basically global scope. No need to call window.functionname() Basically EVERYTHING you define without scope is defined in window. :) Try in this code: function test(){}; console.log('what?', window.test);
1

In this case your function is visible only within the anonimous function you defined in load. You should attach an onclick event to the button using the jquery click inside the load (and also remove onclick attribute):

  $('#buttonid').click(function(){practice();});

1 Comment

If not mine, a hybrid from ours could be ok also: $(document).load(function(){ $('#buttonid').click(function(){practice();}); });

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.