0

I'm trying to create a to-do list app but i cant seems to append the user data to my list

html:

 <div id="Html">
         <input id="input-task" placeholder="New Task" >
              <button id="add-btn" type="button" onclick="newTask()">
                    Add Tasks
              </button>
     </div>
    <div>    
    <ul id="task-table">
        <li>Task 1</li>
    </ul>
    </div>

separate javascript file:

 const inputTask = document.getElementById('input-task').value;

   
    var li = document.createElement('li');
        var t = document.createTextNode('inputTask');
        li.appendChild(t);
        document.getElementById('task-table').appendChild(li);
}

3 Answers 3

2

inputTask is variable containing the text value but you using that as if it is string, remove the quotes around it.

I will also suggest you to avoid inline event handler, you can use addEventListener() instead like the following way:

document.getElementById('add-btn').addEventListener('click', newTask);

function newTask(){
  const inputTask = document.getElementById('input-task').value;

  var li = document.createElement('li');
  var t = document.createTextNode(inputTask); //remove quotes here
  li.appendChild(t);
  document.getElementById('task-table').appendChild(li);
}
<div id="Html">
  <input id="input-task" placeholder="New Task" >
  <button id="add-btn" type="button">
    Add Tasks
  </button>
</div>
<div>    
  <ul id="task-table">
    <li>Task 1</li>
  </ul>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for help...so its bests to set variables inside the function your using??
1

Try like this. You should call newTask function and append child in it.

const inputTask = document.getElementById('input-task').value;

function newTask(){
    var li = document.createElement('li');
    var t = document.createTextNode(document.getElementById('input-task').value);
    li.appendChild(t);
    document.getElementById('task-table').appendChild(li);
    document.getElementById('input-task').value="";
}
<div id="Html">
     <input id="input-task" placeholder="New Task" >
          <button id="add-btn" type="button" onclick="newTask()">
                Add Tasks
          </button>
 </div>
<div>    
<ul id="task-table">
    <li>Task 1</li>
</ul>
</div>

2 Comments

thank you so much....so my first const inputTask variables didnt go into the newTask function?
Well the variable inputTask will initialized as empty string when the page is loading. And it is never updated. So you should get the value in newTask() function.
1

Nothing wrong in your code! Simple fixed this line

var t = document.createTextNode('inputTask');

Remove ('' quotes)

var t = document.createTextNode(inputTask);

And Final thing I don't know may be you have already done this but I want to mention this Wrap this code inside the newTask() function Example: -

function newTask(){
  const inputTask = document.getElementById('input-task').value;   
  var li = document.createElement('li');
  var t = document.createTextNode(inputTask);
  li.appendChild(t);
  document.getElementById('task-table').appendChild(li);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.