jquery - javascript code is not executing correctly locally, but works in JS Fiddle -


i have simple html file looks this:

<html>     <head>         <script src="jquery-1.11.1.min.js"></script>         <script src="mainscript.js"></script>     </head>     <body>         <button id = "thebutton" type="button">click me!</button>     </body> </html>  

mainscript.js looks this:

$("#thebutton").click(function() {   alert( "handler .click() called." ); }); 

nothing happens when click though. if put alert js file happen. if run snippet of js in console, when click button work. i've tested jquery loaded, , is. i've put code in js fiddle , works (http://jsfiddle.net/ds59ruvu/). stupid thing doing locally that's preventing working?

wrap code inside ready handler:

$(document).ready(function(){   // code here }); 

or,

$(function(){   //your code here }); 

or, move script file before closing of body:

<html>     <head>      </head>     <body>         <button id = "thebutton" type="button">click me!</button>         <script src="jquery-1.11.1.min.js"></script>         <script src="mainscript.js"></script>     </body> </html>  

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -