1

I am trying to make a basic to do application.

Here is what I have done so far;

  • When the user clicks a button a prompt appears asking the user to enter a task.

  • The task will then be stored in an array

  • I have displayed the array in the console. However, I am having trouble displaying the array on the web page:

var toDoItems = [];
var parsed = "";

document.getElementById("addItem").onclick = function() {
  var userInput = prompt("Enter your Todo: ")
  toDoItems.push = userInput;
  console.log(toDoItems);
}
<!DOCTYPE html>
<html lang="en">

<head>
  <link href='https://fonts.googleapis.com/css?family=Lato:100,300,400,300italic' rel="stylesheet" type="text/css">
  <link rel="stylesheet" type="text/css" href="css/style.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <title>To Do List</title>
</head>

<body>
  <h1>My Tasks</h1>
  <button id="addItem">Add item</button>
  <div id="item-list">
  </div>
  <script src="js/script.js"></script>
</body>

</html>

5
  • You can display the array list like this : $('#item-list').html(toDoItems.toString()); This will give you comma seperated values of that array Commented Aug 9, 2018 at 12:24
  • Loop over the array. Inside the loop, create a html element from the todo item. Insert the created html into the page. Commented Aug 9, 2018 at 12:24
  • $() will only work if he's actually using JQuery, which this question isn't tagged as. Commented Aug 9, 2018 at 12:24
  • He's included a link to jquery in his head Commented Aug 9, 2018 at 12:26
  • Just as a side note: Array.push() is a function so you have to write it like this: toDoItems.push(userInput); Commented Aug 9, 2018 at 12:30

6 Answers 6

5

The first thing is you need to use Array.push() and not Array.push = someVal. Then you can loop over to the values and create a list of elements in the HTML:

var toDoItems = [];
var parsed = "";

document.getElementById("addItem").onclick = function() {
  var nHTML = '';
  var userInput = prompt("Enter your Todo: ")

  toDoItems.push(userInput);
  toDoItems.forEach(function(item) {
    nHTML += '<li>' + item + '</li>';
  });

  document.getElementById("item-list").innerHTML = '<ul>' + nHTML + '</ul>'
}
<head>
  <link href='https://fonts.googleapis.com/css?family=Lato:100,300,400,300italic' rel="stylesheet" type="text/css">
  <link rel="stylesheet" type="text/css" href="css/style.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <title>To Do List</title>
</head>

<body>
  <h1>My Tasks</h1>
  <button id="addItem">Add item</button>
  <div id="item-list">
  </div>
  <script src="js/script.js"></script>
</body>

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

3 Comments

+1 for an easy to read answer. Also in case you didn't know: Code-Snippets have an inbuild autoformat button: i.sstatic.net/pI5af.png
@FabianN. you are correct. I did not know that :). Thanks
It took more than a week of reviewing edit suggestions and first posts until I started suspecting that people in general (not just new users) just don't know about the button or better, they don't recognize it as the auto format code-button... So I made a picture of it... But you are the first one who confirmed my suspicion =)
3

You can use map() to map over the toDoItems and convert each items to html code and then use join() to combine the array into one string of HTML code.

Like this:

const HTML = toDoItems.map( item => `<li>${item}</li> ` ).join('');
document.getElementById("item-list").innerHTML = '<ul>' + HTML + '</ul>'

Edit: Fixed typo (should be join, not joint)

Comments

1

Loop over toDoItems, create a <p> tag and append it to #item-list:

toDoItems.forEach((item) => {
    let p = document.createElement('p');
    p.innerText = item;
    document.querySelector('#item-list').appendChild(p); 
});

Comments

1

You can use innerHTML for this

document.getElementById("item-list").innerHTML += "<p>" +userInput+"</p>";

demo : plunker

Comments

0

Check below I think this may help you
I removed style from your code for my convenience

<html>
    <head>
        <link href='https://fonts.googleapis.com/css?family=Lato:100,300,400,300italic' rel="stylesheet" type="text/css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <title>To Do List</title>
        <script>
        function myFunction(){
        var toDoItems = [];
         var parsed ="";
        var userInput = prompt("Enter your Todo: ")
        toDoItems.push = userInput;
        document.getElementById("item-list").innerHTML=userInput;
        console.log(toDoItems);
    }
        </script>
    </head>
    <body>
        <h1>My Tasks</h1>
        <button id="addItem" onclick="myFunction()">Add item</button>
        <div id="item-list">
        </div>
    </body>
    </html>

Comments

-1

The Code above has an drawback: It erases the old value of userInput when you enter a new value in prompt(). I propose: document.getElementById("item-list").innerHTML +=userInput+"
"; Vu

3 Comments

It's me: += userInput+"<br />"; (the last line)
Hello, please see meta.stackoverflow.com/editing-help Thanks!
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.