Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsFiddle: no connection between html and js? Can't call simple function from button?

Sorry if this is incredibly obvious, but I have no idea why this isn't working.

I'm trying to create a jsFiddle. However, I don't seem to be able to attach any of the javscript to any of the elements. For example, onclick is not calling my js function.

It doesn't get any simpler than this fiddle. I copy-pasted it from the W3 editor where it works just fine - click the button, get alert!

<button onclick="myFunction()">Click me</button>

function myFunction() {alert("sss");}

http://jsfiddle.net/tpip/NYkQN/2/

What am I missing here??

like image 997
user984003 Avatar asked Jan 24 '13 11:01

user984003


People also ask

How do I run a function in JSFiddle?

With JSFiddle, you can write code in the JavaScript box and then execute it by clicking the Run button on the menu bar at the top of the JSFiddle page.

How do I get JSFiddle code?

In the very own, for example, http://jsfiddle.net/Ua8Cv , if you just type the extra /show at the address bar and hit enter (followed by the browser show source code shortcut) to get the source.

What is https JSFiddle net?

JSFiddle is an online IDE service and online community for testing and showcasing user-created and collaborational HTML, CSS and JavaScript code snippets, known as 'fiddles'. It allows for simulated AJAX calls.


1 Answers

Just change the way the JavaScript block is loaded to no wrap (head)

enter image description here

The standard setting here is that the JS is wrapped(!) to be run after the DOM has loaded. That means the function is not defined in global scope but inside the wrapper. Inline event handlers only work with globally defined functions though!

When setting to no wrap (head) the JS is loaded in the <head> and is not wrapped.

like image 92
Sirko Avatar answered Oct 17 '22 20:10

Sirko