nmsdvid
nmsdvid

Reputation: 2898

JavaScript onclick event is not working

A simple JavaScript onclick event is not working but I don't understand why, here is the code:

<button onclick="removeLol()">remove style</button>

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

and here is a simple example: http://jsfiddle.net/YNQg6/1/

Upvotes: 1

Views: 1478

Answers (2)

jantimon
jantimon

Reputation: 38142

That's only because of the way how jsfiddle inserts the javascript.

Jsfiddle wraps your code into a function which limits the scope and protects the global namespace:

window.onload=function(){
  function removeLol() {
    alert("hello");
    element.className = element.className.replace(" lol ", "");
  }
}

If you tell jsfiddle to inject the code into the document body it works:

http://jsfiddle.net/YNQg6/13/

Or you could turn your "local" function in a global one:

window.removeLol = removeLol;

Upvotes: 4

What have you tried
What have you tried

Reputation: 11138

The code works just fine (just tried it on my server). Check your console for other errors that may be causing the script to not run

Upvotes: 0

Related Questions