Saturday, June 1, 2024
 Popular · Latest · Hot · Upcoming
96
rated 0 times [  98] [ 2]  / answers: 1 / hits: 15801  / 10 Years ago, tue, october 7, 2014, 12:00:00

Attempting my first Javascript project, playing around with DOM to make a To-Do List.
After adding an item, how do i get the 'Remove' button to function and remove the item + the remove button.
Furthermore, after a new entry is made, the list item still stays in the input field after being added. How can it be made to be blank after each list item.



And yes i know my code is kinda messy and there is most likely an easier way to create it but I understand it like this for now.



Any help is greatly appreciated. Thanks



JSFiddle Link : http://jsfiddle.net/Renay/g79ssyqv/3/



<p id=addTask> <b><u> Tasks </u></b> </p>
<input type='text' id='inputTask'/>
<input type='button' onclick='addText()' value='Add To List'/>


function addText(){
var input = document.getElementById('inputTask').value;
var node=document.createElement(p);
var textnode=document.createTextNode(input);
node.appendChild(textnode);
document.getElementById('addTask').appendChild(node);

var removeTask = document.createElement('input');
removeTask.setAttribute('type', 'button');
removeTask.setAttribute(value, Remove);
removeTask.setAttribute(id, removeButton);
node.appendChild(removeTask);

}

More From » dom

 Answers
24

You can simply assign event:



 removeTask.addEventListener('click', function(e) {
node.parentNode.removeChild(node);
});


http://jsfiddle.net/g79ssyqv/6/


[#69212] Sunday, October 5, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alexandreah

Total Points: 720
Total Questions: 85
Total Answers: 90

Location: Central African Republic
Member since Fri, Jun 5, 2020
4 Years ago
;