Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does jsfiddle throw error that function is not defined? [duplicate]

The example I was testing is very simple: just one button, and when button is clicked then a function will be called. However, I'm getting:

error ReferenceError: AddNewData is not defined {"error": "Please use POST request"}

Here is the jsfiddle link: http://jsfiddle.net/tridip/62Ls6x9n/158/

<button onClick="javascript:AddNewData();return false;">Add New Data</button>
function AddNewData()
{
    alert("test");
}

Can someone tell me what is wrong there?

like image 922
Thomas Avatar asked Dec 25 '22 18:12

Thomas


1 Answers

It's because by default jsfiddle uses onLoad JS definition for your code.

That's it - it's wrapped in an anonymous function so all symbols (variables, functions, etc) are scoped within.

Change it to No wrap - in <head>

http://jsfiddle.net/62Ls6x9n/160/

like image 129
zerkms Avatar answered Dec 28 '22 10:12

zerkms