Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript function not being called in "fiddle" [closed]

I made this short fiddle: http://jsfiddle.net/NZnN2/. Why is it not popping up the alert when I click the button?

like image 487
user1365010 Avatar asked May 08 '12 17:05

user1365010


2 Answers

Because your JavaScript code is in an onload handler, which means display_message is not global, and therefore not accessible by the HTML.

Since you selected onLoad, your JavaScript is injected into the page like this:

window.addEvent('load', function() {
    function display_message(){
        alert("woohoo!");
    }
});

As you can see, display_message is only accessible inside that anonymous function. To make the example work, change onLoad to no wrap (head) or no wrap (body) (on the left side of the page).

Working example: http://jsfiddle.net/NZnN2/8/

like image 64
Rocket Hazmat Avatar answered Nov 11 '22 19:11

Rocket Hazmat


instead of

function display_message() {
    alert('woohoo!');
}

do

display_message = function () {
    alert('woohoo!');
}
like image 20
Keil Avatar answered Nov 11 '22 18:11

Keil