I try to delete an object from an array but I can't access the array for some reason. How can I get the array object in the delete_task function?
In task.tag.html
<task>
<button onclick={opts.delete_task}>{ title }</button>
</task>
In tasklist.tag.html
<tasklist>
<todo title="test" delete_task={delete_task}></todo>
<script>
this.delete_task = function () {
for (var i = tasks.length - 1; i >= 0; i--) {
if (tasks[i].title === this.item.title) {
tasks.splice(i, 1);
}
}
};
this.tasks = [{title: "test"}]
</script>
</tasklist>
You can use
parent
word, according to the nested loops approachAnd the
item
from the event, according to the events handlingIn the result you could have something like that: https://jsfiddle.net/relaximus/q4t8e0sc/
Actually, as the click event is bubbling up, you can use
onclick
on the whole task tag, in this case you will not need to pass the delete handler as a function in the tag.And just write