jquery - javascript code is not executing correctly locally, but works in JS Fiddle -
this question has answer here:
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
Post a Comment