1

So I am learning JavaScript.... I have gone way past this basic stuff in my learning but can't figure out why this wont work:

   function message(){
      alert("message"); 
    }

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

On Js Bin all is ok http://jsbin.com/uwutut/1/edit

On Js fiddle the alert is not triggered!! http://jsfiddle.net/vincentieo/D2dxA/

It is the exact same code...copy and paste so no reason why js fiddle is being fussy??

zod
  • 12,092
  • 24
  • 70
  • 106
vincentieo
  • 940
  • 2
  • 13
  • 28

2 Answers2

4

The jsfiddle infrastructure puts your code in an event handler for the "load" event if you don't tell it not to. That means that your function is a local function to that handler, so it's not visible globally.

On the left side of the jsfiddle UI, look for a pull-down about where the code goes, and choose "no-wrap (body)". See if that helps.

Pointy
  • 405,095
  • 59
  • 585
  • 614
0

it works perfectly if you move your javascript to your header

http://jsfiddle.net/D2dxA/2/

<head>
<script>
function message(){
  alert("message"); 
}
</script>
</head>
Mehdi Karamosly
  • 5,388
  • 2
  • 32
  • 50