0

HTML

<div id="test"></div>
<input type="button" value="Go" onclick="test()" />

JavaScript

function test() {
    alert("Test!");
}

Fiddle

http://jsfiddle.net/MVBrS/11/

Phillip Berger
  • 2,317
  • 1
  • 11
  • 30

2 Answers2

0

Please look at this one, it is about jsfiddle code frames separation:

Inline event handler not working in JSFiddle

Of course, if you were running the same code embedded on plain HTML it works normally, having the alerted popup appearing.

<!DOCTYPE html>
  <html>
    <head>
      <script>
        function test() {
          alert("Test!");
        }
      </script>
     </head>
     <body>
       <div id="test"></div>
       <input type="button" value="Go" onclick="test()" />
     </body>
  </html>
Community
  • 1
  • 1
Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54
-2

when you do

onclick="test()"

as an attribute of the input element, you are setting the result of the call test() (in your case 'null') as the click event handler

you probably want to do this

onclick="test"

instead, which will set the actual 'test' function as the handler,

or even better, follow the following guidelines: unbtrusive javascript or unobtrusive JS (2),.. you get the point ;)

Community
  • 1
  • 1
Francisco Meza
  • 875
  • 6
  • 8