-1

I am newbie in jsfiddle

I have put a very simple javascript code in jsfiddle http://jsfiddle.net/5bMSp/1/

<script>
function alertme(){
    alert("HI");
}
</script>
<a href="#" onclick="alertme()">click</a>

This simple code works awesome in any browser but not in jsfiddle. Am I missing anything in the jsfiddle?

veer7
  • 20,074
  • 9
  • 46
  • 74
  • 3
    and [Simple example doesn't work on JSFiddle](http://stackoverflow.com/q/5431351/218196) – Felix Kling May 02 '14 at 15:16
  • 1
    Change the drop down on the left side from `onLoad` to `no wrap - in ` – j08691 May 02 '14 at 15:17
  • As many of js developer won't expect `javascript` code to be wrapped in `onLoad` by default, the dropdown should be `no wrap` by defualt. Isn't that a `bug` in jsfiddle? – veer7 May 02 '14 at 15:28

1 Answers1

4

By default, jsFiddle puts all of your code within an onload handler, like this:

window.onload = function() {
    // ...your code here...
};

That means your alertme function isn't a global, and it has to be for onclick attributes to work.

There's a drop-down on the left, near the top, that controls this behavior. Change it from onLoad to one of the "no wrap" options to make your function global.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • great... I have been struggling with this for long time. As many of js developer won't expect `javascript` code to be wrapped in `onLoad` by default, the dropdown should be `no wrap` by defualt. Isn't that a `bug` in jsfiddle? – veer7 May 02 '14 at 15:23
  • @veer7: I've been known to call it *"...a **surprising** default..."* :-) In my view it should default to "No wrap - end of body." But hey, I didn't build it and don't maintain it, so I don't really get a say. :-) – T.J. Crowder May 02 '14 at 15:30