19

There is some code I wanted to put into JSFiddle. It didn’t work. Narrowing it down I can’t even get this simplest of code to work:

JSFiddle

function displaymessage() {
  alert("Hello World!");
}
<form>
  <input type="button" value="Click me!" onclick="displaymessage()" />
</form>

<p>By pressing the button above, a function will be called. The function will alert a message.</p>

The alert box doesn’t show up in the JSFiddle.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
thomasf1
  • 1,782
  • 4
  • 18
  • 32

8 Answers8

36

Select No wrap - bottom of <head> in the “Load type” dropdown in the JavaScript settings.

Screenshot of the dropdown mentioned above

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Igor Dymov
  • 16,230
  • 5
  • 50
  • 56
9

You need to take your function out of the onLoad/onReady otherwise it is placed inside of another scope and your button cannot access the function. In your case you need to use No wrap (head)

The code generated looks like this:

Ext.onReady(function() {
    function displaymessage()
    {
        alert("Hello World!");
    }
});
John Kalberer
  • 5,690
  • 1
  • 23
  • 27
4

Change the code to run "no wrap (head)" instead of "onDomReady". Your function isn't visible to your markup as is.

FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
1

Maybe some of you need to move your function outside of document.ready if you use html onclick="func()"

$(document).ready(function() {
  ...
}

function func(){
  ...
}
0

If you still have problems, check whether you have

  1. AdBlockers installed as a browser extension? (i.e. AdBlocker Plus)

enter image description here

  1. Turn off AdBlockers in JSFiddle and run again.

I hope this helps some one.

Thanks.

Anjana Silva
  • 8,353
  • 4
  • 51
  • 54
-2

Sorry but... is much simpler than what you propose...

If you want to call a js function with "onclick" attribute, put your javascrit code directly in head, you dont need domready or similars.

Second, you need to include "javascript:" before the function name.

See: http://jsfiddle.net/XNJxT/1838/

<input type="button" value="Click me!" onclick="javascript:displaymessage()" />
Korreca
  • 13
  • 1
  • 4
    As stated [in this beautiful answer](http://stackoverflow.com/a/11348403/1908115) the `javascript:` is not recommended (bad practice). – erik Mar 13 '14 at 10:08
-4

My problem to this was when I made my script tag I typed it out :

script type="javascript/text"

instead of :

script type="text/javascript"

and now it works.

HMD
  • 2,202
  • 6
  • 24
  • 37
zNiine
  • 1
-6

http://jsfiddle.net/praveen_prasad/XNJxT/14/

Js fiddle so something like this to whatever you write

window.addEvent('load',function(){

//Your code


});

enter image description here

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Praveen Prasad
  • 31,561
  • 18
  • 73
  • 106