0

I have put this js code into visual studio and it wont work. Please help:

var task = document.getElementById('action');
var button = document.getElementById('button');
var err = document.getElementById('err');
var list = document.getElementById('tasks');

button.addEventListener("click", function() {
  if (task.value == "") {
    err.style.backgroundColor = 'red';
    err.innerHTML = "Please enter a value";
  } else {
    var newItem = document.createElement("li");
    newItem.innerHTML = task.nodeValue;
    list.appendChild(newItem);
    err.style.backgroundColor = "none";
    err.innerHTML = "";
    task.nodeValue = "";
  }
})
<header id="header" style="background-color:green;">
  <div id="title">To-Do</div>
  <div id="date"></div>
</header>
<br>
<hr>
<div id="mains">
  <p id="err"></p>
  <label for="text">Task: </label>
  <input type="text" id="action" name="text" placeholder="Wash the dishes..."><br>
  <button id=button>Add</button>
  <ul id="tasks"></ul>
</div>

when i press 'add' nothing happens the text remains and ul is unchanged i tried to copy from a post but that also didnt work but was essentially the same as shown above

a.mola
  • 3,883
  • 7
  • 23

1 Answers1

0

I guess, the problem in this line:

 newItem.innerHTML = task.nodeValue;

It will work if you'll replace task.nodeValue with task.value;

According to the documentation your task.nodeValue will be always null. And moreover, in your code the "if" statement checks task.value

Roman Azarov
  • 101
  • 8
  • [*"replacing nodeValue with value doesnt work so"*](https://stackoverflow.com/questions/68641928/my-code-for-appending-an-item-onto-a-ul-isnt-working-javascript?noredirect=1#comment121309413_68641928) - this is clearly not the problem. – Spectric Aug 03 '21 at 20:56
  • just check a fiddle from SkillGG's answer. It works – Roman Azarov Aug 03 '21 at 22:15