How to add li to a ul via javascript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to add li to a ul via javascript?

Hi, I was learning JS and wanna make a simple To-do list that will add items every time the form is submitted. The problem is new item is replacing the old item, how to resolve this issue? please see my code and let me know where I am doing it wrong. Please check the code through the following link. https://www.sololearn.com/compiler-playground/WmeeqO84JOqr

25th May 2023, 7:00 AM
AspirantCoder
5 Answers
+ 5
AspirantCoder don't share the code in description go to insert button -> insert code -> see the drop-down menu -> click on code bits then share your code And before you ask the question please do search on google https://stackoverflow.com/questions/20673959/how-to-add-new-li-to-ul-onclick-with-javascript https://code.sololearn.com/W0uW3Wks8UBk/?ref=app
25th May 2023, 7:04 AM
Sakshi
Sakshi - avatar
+ 4
post your codebit. Are you using createElement an appendChild? or if you are using innerHTML and template literal strings, maybe you should use += instead of just =
25th May 2023, 7:02 AM
Bob_Li
Bob_Li - avatar
+ 1
@Sakshi sorry for the wrong post came back after a long time and don't know how to post it properly. I had already checked the article you linked it might have my solution but the problem is I am new to coding so didn't get it properly so thought someone might explain a bit but I got it, I won't post any questions again because Stackoverflow has already got the related solution of almost every problem so beginners need to do it on there own. Thanks btw for taking the time to reply.
25th May 2023, 9:16 AM
AspirantCoder
+ 1
AspirantCoder no problem
25th May 2023, 5:26 PM
Sakshi
Sakshi - avatar
0
You are close! Just move line 2 of your code to the form event handler. This will ensure that a new li element is created whenever the button is clicked.
25th May 2023, 10:08 AM
ODLNT
ODLNT - avatar