0

This is the most frustrating error I have probably ever had while coding. I'm trying to click a button and have it pop up "hello" in an alert box. If I remove the document ready it works. If I remove the function and just have the alert box in the document.ready, it also works but obviously without a click. However, that JS doesn't work all together and I have no idea why.

HTML

<button id="signup-box-button" type="button" onclick="signupSubmit()">sign up</button>

JS

$(document).ready(function(){

    function signupSubmit(){
        alert("hello");
    }

});
4
  • Perhaps you are looking to put this inside your document.ready block?: $('#signup-box-button').on('click', signupSubmit); Commented Aug 20, 2015 at 2:57
  • If you put the function inside a document ready handler, it won't exist when it is being bound to the HTML Commented Aug 20, 2015 at 2:58
  • 2
    You really should stop using inline event handlers - especially so, considering you have jQuery's fleet of event handling options at your disposal. Commented Aug 20, 2015 at 3:03
  • I feel really stupid right now... I haven't coded in a few months and I don't know why I was determined to make it be inside the document.ready Commented Aug 20, 2015 at 3:03

2 Answers 2

4

When you put a function or variable inside a function its scope is limited to that function, this means that it will be visible only when inside that function.

To solve this, just move the function that is used outside of "ready function" to be visible outside.

$(document).ready(function(){
    // code here
});

function signupSubmit(){
    alert("hello");
}

Or, better yet. Leave Javascript code to Javascript file only, and let the HTML with HTML only.

HTML

<button id="signup-box-button" type="button">sign up</button>

Javascript

$(document).ready(function(){
    $('#signup-box-button').click(function(){
        alert("hello");
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

This is because of scoping in javaScript as explained by Skarllot

Please refer to know how this keyword works with inline event handlers.

You can use the below code snippet

$(document).ready(function(){

     this.signupSubmit = function(){
        alert("hello");
    }

});

Refer JSFIDDLE

By adding this keyword the function will be registered in global scope. And the above solution may pollute the global namespace.

To better understand the above functionality:

Inline event handlers are evaluated in global scope. In your first example, signupSubmit is not defined in global scope, it is defined inside the function you pass to $(document).ready. Hence the inline event handler cannot find the function.

Here is a simplified example:

function ready() {
    function signupSubmit() { }
}

ready();
signupSubmit(); // ReferenceError because signupSubmit is not defined in this scope

Note that there is no reason to put function definitions inside a $(document).ready callback, except for explicitly not putting them in global scope by using this keyword as mentioned in my first example.

5 Comments

This do not seems to be a good practice. I would not recommend it.
Please refer quirksmode.org/js/this.html when you are using inline event handler. And also can you please explain why it is not a good practice ? I am quite new to this
Although you is not polluting the global scope, you still polluting HTML with Javascript code.
I agree that the global namespace will be polluted. I did not understand how the HTML and Javascript code is getting polluted can you please explain ? Probably with an example
onclick="signupSubmit()" is a Javascript code on HTML code. See the second part of my answer as example.

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.