-4

Why is this simple onclick JavaScript function not called in JSFiddle ?

HTML:

<input type="button" value="Check" id="zeCheck" onclick="check();" />

JS:

function check() {
    alert('ech');
}

DEMO

myTerminal
  • 1,596
  • 1
  • 14
  • 31
franchez
  • 567
  • 1
  • 7
  • 20
  • the console says: Uncaught ReferenceError: check is not defined – Alex Feb 26 '14 at 13:41
  • 6
    Change the dropdown that says 'onLoad' to 'No wrap in body' – putvande Feb 26 '14 at 13:41
  • same as below. requested to merge: http://stackoverflow.com/questions/5431351/simple-example-doesnt-work-on-jsfiddle hope it helps – Shourya Sharma Feb 26 '14 at 13:51
  • @putvande thanks for this. multiples down votes, sorry was just not aware about fiddle not finding my javascript if dropdown options not changed to 'no wrap' ... – franchez Feb 26 '14 at 13:54

4 Answers4

1

It doesn't work because you are defining this function in wrong place...

Put your script function in .js file or at the end of document in <script></script> tags.

Check this fiddle

Flash Thunder
  • 11,672
  • 8
  • 47
  • 91
0

Take it out from the onload event and it will work. Put it in the body. (left panel)

Bruno
  • 115
  • 2
  • 10
0

onLoad in JS FIDDLE is the same as window.onload=function() in JavaScript .

No wrap - in is the same as

<head><script type="text/javascript"> //your
//code goes here</script</head>

So you just have to change it to NO wrap to body.

Vinayak Pingale
  • 1,315
  • 8
  • 19
0

The function is being defined inside a load handler and thus is in a different scope. You can fix this by explicitly defining it on the window object. Better, yet, change it to apply the handler to the object unobtrusively: http://jsfiddle.net/pUeue/

$('input[type=button]').click( function() {
   alert("test");   
});

Note applying the handler this way, instead of inline, keeps your HTML clean. I'm using jQuery, but you could do it with with or without a framework or using a different framework, if you like.

Shourya Sharma
  • 547
  • 4
  • 23