I'm having trouble triggering getjson success event. When I call $ .getJSON on $ (document). Already works, and when I put the same code when I click the button, it does not work.
Works fine (under $ (document) .ready)
<html>
<head>
<title>API Logger</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.min.js" ></script>
<script>
"use strict";
$(document).ready(function(){
var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
$.getJSON( flickerAPI,
{
tags: "mount everest",
tagmode: "any",
format: "json"
},
function(data)
{
alert("success");
});
});
</script>
</head>
<body>
<form>
<button id="btn1" >Execute</button>
</form>
</body>
Doesn't work (under $ ('# btn1'). On ('click', function ()
<html>
<head>
<title>API Logger</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.min.js" ></script>
<script>
"use strict";
$(document).ready(function(){
$('#btn1').on('click', function() {
var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
$.getJSON( flickerAPI,
{
tags: "mount everest",
tagmode: "any",
format: "json"
},
function(data)
{
alert("success");
});
});
});
</script>
</head>
<body>
<form>
<button id="btn1" >Execute</button>
</form>
</body>
source
share