It's my first question here so please bear with me. I'm coding a piece of a web app that gets the content of another webpage with $.get and then assigns it to an array. The returned content of the web page is the typical JS array (The following is literally the return of the $.get):
[{"id":2476,"name":"XXXX","start_date":"2018-06-06 00:00:00","end_date":"2018-06-06 23:59:59","current_attendees":2442}]
The thing is that it seems to be assigning it as a string (An alert shows the whole string just like the one above) and a later events.length says it is undefined. Bottom line, how can I make Javascript recognize that it is an array or not a string? Thanks. The code:
<script>
function upcomingevents(){
var events, id, place, date;
id = <?php if($spotlightevent != null ){echo $spotlightevent;}else{echo 0;} ?>;
maineventplace = 0;
$.get("THERE IS A LINK HERE", function(data){
events=data;
alert(events);
}, "HTML");
if( id != null){
for(i = 0; i = events.length; i++){ //Throws "Can't get length of undefined"
if(events[i]["id"] == id){
$("#maineventtitle").text(events[i]["name"]);
$("#maineventcount").text("Inscritos: " + events[i]["current_attendees"]);
$("#maineventsponsor").text("Valor Patrocínios: " + <?php echo $spotlighteventsponsor; ?> + "€" );
maineventplace = i;
}
}
}
for(i = 0; i = events.length; i++){
if(events[i]["id"] != id && i != maineventplace){
$("#secondeventtitle").text(events[i]["name"]);
$("#secondneventcount").text("Inscritos: " + events[i]["current_attendees"]);
}
}
}
setInterval(upcomingevents(), 30000);
</script>