I am learning blockchain with truffle by trying to build a simple TODO LIST project. At first, I could not successfully disable the display of task items in my todo list when clicking on X symbol.... now I accidently find out the solution but just can't figure out why and how it works....
my solution is as below codes( especially in the bottom, start from the comment //when click to X, list disappear)
my problems are :
why can't I find the tags of li, span, close outside of this for loop (the return of console.log was null) with getElementsByClassName method while finding ul tag is possible ??
Even I can only find those tags within for loop, why this for loop always running and waiting for me to click X?? Isn't the for loop supposed to be terminated after reiterating through the number of times we asked them to reiterate??!!
render: function() {
//Load account data
if(web3.currentProvider.enable){
//For metamask
web3.currentProvider.enable().then(function(acc){
App.account = acc[0];
$("#accountAddress").html("Your Account: " + App.account);
});
}
else{
App.account = web3.eth.accounts[0];
$("#accountAddress").html("Your Account: " + App.account);
}
// Load contract data
App.contracts.myTodoList.deployed().then(function(instance) {
myTodoListInstance = instance;
return myTodoListInstance.TasksCount();
}).then(function(TasksCount){
var shownList = document.getElementById("todo-list")
TasksCount = TasksCount.toNumber();
for (var i = 1; i <= TasksCount; i++) {
myTodoListInstance.Tasks(i).then(function(task) {
var id = task[0];
var content = task[1];
let completed = task[2];
// create li node inside of for loop so that you won't appendChild the same 'li' over and over again
var newTaskNode = document.createElement('li');
newTaskNode.innerHTML = content;
// Add a "X" symbol in the end of each listed task
var span = document.createElement("SPAN");
var txt = document.createTextNode("x");
span.className = "close";
span.appendChild(txt);
newTaskNode.appendChild(span);
// Render initial tasks Result
shownList.appendChild(newTaskNode);
// when click to X, list disappear
//(don't know how it worked... why can't i locate 'li','span','close' outside of this for loop ??)
//(even i can only locate those tags within this loop, why this for loop always running and waiting for me to click X ??!!)
var close = document.getElementsByClassName("close");
var i;
for (i = 0; i < close.length; i++) {
close[i].onclick = function() {
var div = this.parentElement;
div.style.display = "none";
}
}
});
}
});
},