I'm trying to get names from a Pokemon API and putting it into a div field in html, but the .text() function from jQuery doesn't seem to place the text into the html when I put it inside a .getJSON function. Just curious what could this problem be? Thanks.
<div class="container">
<div class="row">
<div class="col-sm-3" id="1">
<div id="name1"></div>
</div>
<div class="col-sm-3" id="2">
<div id="name2"></div>
</div>
<div class="col-sm-3" id="3">
<div id="name3"></div>
</div>
<div class="col-sm-3" id="4">
<div id="name4"></div>
</div>
</div>
</div>
Javascript Code
$(document).ready(function() {
/*Works*/
for(var j = 1; j < 5; j++){
$("#name" + j).text("HELLO");
}
/*Doesn't work*/
for(var j = 1; j < 5; j++){
var webAddress2 = "http://pokeapi.co/api/v1/pokemon/" + j;
$.getJSON(webAddress2, function(data) {
console.log("test");
$("#name" + j).text("SOME TEXT");
});
}
});