Reputation: 677
I am trying to get user repos from github. The code I'm using it isn't working
Here is where I am trying to implement it. http://codeforu.ms/staff/matthew/#ourTab
Its working in this JSFiddle http://jsfiddle.net/wh1t3w0lf21/nT5wY/3/
And here is the code
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$("#btn_get_repos").click(function() {
$.ajax({
type: "GET",
url: "https://api.github.com/users/google/repos",
dataType: "json",
success: function(result) {
for( i in result ) {
$("#repo_list").append(
"<li><a href='" + result[i].html_url + "' target='_blank'>" +
result[i].name + "</a></li>"
);
console.log("i: " + i);
}
console.log(result);
$("#repo_count").append("Total Repos: " + result.length);
}
});
});
</script>
<li id="ourTab" class="profileContent">
<div id="repos">
<ul id="repo_list"></ul>
</div>
<div id="repo_content"></div>
<button id="btn_get_repos">Get Repos</button><span id="repo_count"></span>
</li>
Upvotes: 0
Views: 96
Reputation: 433
I think you are just subscribing to the event before the dom is ready. Just wrap it like this:
$(function() {
$("#btn_get_repos").click(function() {
$.ajax({
type: "GET",
url: "https://api.github.com/users/google/repos",
dataType: "json",
success: function(result) {
for (i in result) {
$("#repo_list").append(
"<li><a href='" + result[i].html_url + "' target='_blank'>" +
result[i].name + "</a></li>"
);
console.log("i: " + i);
}
console.log(result);
$("#repo_count").append("Total Repos: " + result.length);
}
});
});
});
Upvotes: 1