3

I have written a simple function:

function clickedMe() {
  alert(this + " : " + this.tagName + " : " + this.id);
}

And I am calling it on click event of button element

<button onclick="clickedMe()">button</button>

This works when I created a standalone HTML page locally. However it does not seem to work on JSFiddle. Here is the link: http://jsfiddle.net/Mahesha999/fACce/

Whats wrong here?

Mahesha999
  • 22,693
  • 29
  • 116
  • 189

1 Answers1

2

Select no wrap - in <head>/<body> .

It doesn't work because clickedMe is not in the global scope.

enter image description here

Po-Ying Chen
  • 1,690
  • 1
  • 12
  • 12
  • funny enough it seems that this JSFiddle doesnt work with `no wrap` but need `on load`: jsfiddle.net/Mahesha999/fACce/3 Now what if I want both in same JSFiddle? – Mahesha999 Jul 07 '13 at 14:09
  • We have to wait for the page loaded when we use `addEventLisenter` because `document.getElementById` can not get the element before the dom is ready. http://jsfiddle.net/fACce/5/ – Po-Ying Chen Jul 07 '13 at 21:39
  • Yeah that I realized but what if I want both buttons one in original question and one in above comments in same JSFiddle, it worked by explicitly including `clickme()` in HTML ` – Mahesha999 Jul 08 '13 at 06:54