I have a HTML/JS
code as shown below in which on click of Execute button, I want to display:
[{"detail":"Hello World"},{"detail":"Great News"}]
Currently, on clicking Execute button I am getting the following:
[{"detail":""},{"detail":""}]
I am wondering what changes I need to make in the JS code below so that on click of a Execute button, I am able to display:
[{"detail":"Hello World"},{"detail":"Great News"}]
HTML:
<input type="submit" onClick="runCode()" value="Execute" >
<div id="console-log">
</div>
Javascript:
$(document).ready(function() {
})
function runCode(){
var td=new Todo()
td.addTask("Hello World")
td.addTask("Great News")
td.printList()
}
class Todo{
constructor(name) {
this.todolist = [];
this.task={
'detail':''
}
}
addToList(newobj)
{
this.todolist.push(newobj)
}
addTask(taskDetail){
this.task.detail=taskDetail
this.todolist.push(this.task)
this.task.detail='' //clear data for next step
}
printList()
{
var consoleLine = "<p class=\"console-line\"></p>";
var text= JSON.stringify(this.todolist)
$("#console-log").append($(consoleLine).html(text));
//alert(this.todolist)
}
}