cannot set properties of null innerHTML
hi i have 2 problems in script.js of todo app which i learned from freecodecamp .
the first one is when pressing (add) button while the input is empty it supposed to change the text of label of input via a (validation) simple if else statememnt combined with innerHTML but for some reason it consoled (cannot set properties of null innerHTML) .
second when you enter a task and press add it appears but when you add the second task 2 tasks will appear .
https://codepen.io/ilyas-draissia/pen/ogvWBZO
15 Replies
it seems to work in codepen
so, my guess is that you have the script inside the <head>
add the
defer
attribute to the script
or, alternatively, listen to the load
event on the window
The form validation doesn't work in codepen and with vscode liveserver it shows the cannot set properties of null error in console
Plus try entering two tasks in a row ,3 tasks will appear .
the element with id
time
doesn't existYou're right i fixed it thnks but the other problem i didnt figure it out ,i saved it try refreshing so it show up on your pc
line 32
you're adding all the
taskData
every single time
(ignoring how horrible it is to use innerHTML
for this)
so, instead of this, just do this:
Thanks
Instead of innerHTML what should I be using
you should use this amazing tag: <template>
then, when it's time to add stuff to the list, you do a deep clone of the contents, and manipulate everything
you use it like a normal element
then, you just append it to the list
you should check the mdn page about the <template> tag
alright thanks
I love the
<template>
tag!! Just wanted to add that if all you need is a quick <p> tag or something you don’t need <template>
for, you can use document.createElement(‘p’)
But in the case above , you should def rely on templateI wonder if
taskData.forEach(({id,text,date})
is a good code ?depends on the context
where you were using it? YES, very bad because it was wrong
somewhere else? depends
aaa i get it
context is important
in isolation there's nothing wrong with that line though. It's the wrong line for the situation, but not inherently wrong to use in the proper situation
exactly