4

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?

Jeroen
  • 60,696
  • 40
  • 206
  • 339
Thomas
  • 33,544
  • 126
  • 357
  • 626

1 Answers1

10

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/

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • 1
    would mind to elaborate.......things is clear to me what i read your answer. what exactly u r trying to say. – Mou Jun 09 '15 at 14:11
  • It's just that your JavaScript code is by default wrapped in onLoad and doesn't exist in main scope. Changing it to ``nowrap`` switches it off. With ``onload`` it looks something like below: ```window.addEventListener('load', function() { function AddNewData() { alert("test"); } }, false);``` – zalun Jun 10 '15 at 14:40
  • Your fiddle doesn't even solve the issue, clicking "Update Data" returns `Uncaught TypeError: UpdateData is not a function`. That was the problem to begin with I thought. – Brian Johnson Jun 25 '15 at 16:12
  • @BrianJohnson I'm sure you haven't read the question. And if you have - please bother reading one more time, then repeat, until you get it. Thanks. – zerkms Jun 25 '15 at 19:53
  • 2
    The literal question is: "Why does jsfiddle throw error that function is not defined?" Is your Fiddle still throwing an error that a function is not defined? Yes. Perhaps you could try re-reading the question instead of just being wildly condescending. You solved half the problem, and in this case: the half pertaining to the error he happened to paste. But the very next function, which was also included in his fiddle, is still having the same problem. Perhaps you could update your fiddle to fix that one as well, and explain how you did it if the solution is different. – Brian Johnson Jun 29 '15 at 15:38
  • @BrianJohnson "is still having the same problem" --- it's not the same, not even close. You would have known that, if you've read the code (have you even read the code? do you understand the difference between the original and the "current" problem?) You probably are missing the point of SO - here we **help** solving people their problems, but not volunteer to work for someone for free. As you may see, the OP has checked this answer, which means their problem was solved and they have figured out what is wrong with their code. – zerkms Jun 29 '15 at 20:17
  • @BrianJohnson anyway: 1. You have downvoted me 2. There is an "Add Another Answer" button below - go for it. – zerkms Jun 29 '15 at 20:18
  • 2
    What does "It" in this part of your answer mean? "Change it to No wrap - in " I see no head tag??? – Jim Paul Oct 11 '16 at 19:46
  • @JimPaul click on the "Javascript" label with a wrench, then choose the corresponding dropdown value. A year ago jsfiddle had slightly different design and my instructions were more obvious (that dropdown was on the left bar). – zerkms Oct 11 '16 at 19:54