javascript - Can't add jQuery handler to HTML site -


i have single page site:

<!doctype html> <html>  <head>     <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.5/leaflet.css" />     <link type="text/css" rel="stylesheet" href="stylesheet.css" />     <script src="http://cdn.leafletjs.com/leaflet-0.7.5/leaflet.js"></script> </head>  <body>     <h1>hello world</h1>     <p>i'm hosted github pages.</p>     <div id="map"></div>     <form>         <input id="but" type="button" value="about me" />     </form>     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>     <script src=script.js></script> </body>  </html> 

and add handler button #but, , try use script in script.js:

var mapclicked = function() {     alert("map!");     // create map in "map" div, set view given place , zoom     //var map = l.map('map').setview([55.432, 37.654], 13); }  $(document).ready(function() {     $('#but').click(mapclicked()); }); 

but run html in browser , click button, nothing happens.

by adding () function name when referencing function it's name. call function directly , returned value assigned callback of click event. removing () of function name(or rather function call), function referenced correctly , called when click event occurs on element.

change

$('#but').click(mapclicked()); 

to

$('#but').click(mapclicked); // <----- removed () 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -