mercredi 25 mars 2015

updating DOM through ajax call back with Javascript


I'm working on a calendar that will allow users to store events on a given date. when I update the calendar each new month I create new nodes and assign them an id so I will be able to append something to them later like so:



var i = 1;
for(var w in weeks){
var days = weeks[w].getDates();
// days contains normal JavaScript Date objects.

// alert("Week starting on "+days[0]);
var which_week = "week"+i;
i++;
for(var d in days){
console.log(days[d].toISOString());
var tr = document.getElementById(which_week);
if(days[d].getMonth()==month){

var newDay = document.createElement("div");
newDay.appendChild(document.createTextNode(days[d].getDate()));
//alert(newDay.data);
newDay.setAttribute("id", newDay.lastChild.data);
$(tr).append('<td><a class="linky" href="#">'+newDay.lastChild.data+'</a></td>');
}
else{
$(tr).append('<td class="disabledCell"><a class="linky disabledLink" href="#">'+days[d].getDate()+'</a></td>');
}
}
}
getEvents();
}


I modify the nodes:



function ajaxEventCallback(event){
var data = event.target.responseText;
data = JSON.parse(event.target.responseText);
for (var i = 0; i < data.length; i++)
//alert("event: " + data[i].title);
//
var dayOfEvent= data[i].day;
document.getElementById(dayOfEvent).appendChild(data[i].title);

}


I get a "cannot read property append child of null" error on the last line of code. I think it's a scoping issue but I don't know where to begin solving it.





Aucun commentaire:

Enregistrer un commentaire