Skip to content
TheChatArchiveThe Chat Archive
Human-written demonstration (OpenAssistant volunteers)· OpenAssistant (human-written)Hugging Face OpenAssistant Conversations (oasst2)

Build me in HTML, CSS, and JavaScript an extremely simple To-do-list web application

In OpenAssistant data, “assistant” turns were written by human volunteers as demonstrations. They are not model outputs.
  1. User

    Build me in HTML, CSS, and JavaScript an extremely simple To-do-list web application

  2. Assistant

    Gladly! Here's an extremely simple To-do-list web application written in HTML, CSS, and JavaScript:

    <!DOCTYPE html>
    <html lang="en">
    	<head>
    		<meta charset="UTF-8">
    		<meta name="viewport" content="width=device-width, initial-scale=1.0">
    		<title>To-do-list</title>
    		<style media="screen">
    			* {
    				font-size: 1em;
    				color: #000;
    				font-family: Arial;
    			}
    			
    			body {
    				background-color: #ccc;
    			}
    			
    			DIV.container {
    				background-color: #fff;
    				margin: auto;
    				width: 25%;
    				border: 3px solid black;
    				padding: 10px;
    			}
    		</style>
    	</head>
    	<body>
    		<div class="container">
    			<h1>To-Do List:</h1>
    			<form action="#">
    			<input type="text" class="form-control" placeholder="Add new task..."> 
    			<button id="add-task-button" type="submit">Add Task</button></form>
    			<ul id="task-list" class="list-group">
    				<!-- The tasks will be inserted here -->
    			</ul>
    		</div>
    		<script>
    			// Get the element by ID
    			var addTaskButton = document.getElementById("add-task-button");
    			
    			// Add click event listener to the button
    			addTaskButton.addEventListener("click", function () {
    				var inputValue = document.getElementsByClassName("form-control")[0].value;
    				
    				if (inputValue !== "") {
    					var liElement = document.createElement("li");
    					liElement.innerHTML = inputValue;
    					
    					// Adding task to DOM 
    					document.getElementById("task-list").appendChild(liElement);
    					
    					// Clearing form on adding new task
    					document.getElementsByClassName("form-control")[0].value = ""; 
    				}  
    			});
    		</script>
    	</body>
    </html>
    

    Please note: Once the page is closed, all tasks will be lost.

  3. User

    When I tried running that code the first time, the page refreshed as soon as I hit the "Add Task" button, immediately losing the first task I tried to give it. After that it seemed to work ok.

    Could you fix the above bug, and also add the ability to check off completed tasks?

  4. Assistant

    I apologize for the inconvenience. The error is in the following line of code:

    document.getElementById("task-list").appendChild(liElement);
    

    This line of code adds the new task to the end of the task-list ul element. However, the page refreshes before the new task can be added. To fix this, we need to use the insertBefore() method instead of the appendChild() method. The insertBefore() method inserts the new task before the specified element. In this case, we want to insert the new task before the first element in the task-list ul element. So, the code should be changed to the following:

    document.getElementById("task-list").insertBefore(liElement, document.getElementById("task-list").firstChild);

    This code will insert the new task before the first element in the task-list ul element. Once this change is made, the page will no longer refresh when you add a task.

    Here is the updated code:

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>To-Do List</title>
      <style>
        * {
          font-size: 1em;
          color: #000;
          font-family: Arial;
        }
        body {
          background-color: #ccc;
        }
        DIV.container {
          background-color: #fff;
          margin: auto;
          width: 25%;
          border: 3px solid black;
          padding: 10px;
        }
        .completed {
          text-decoration: line-through;
        }
      </style>
    </head>
    <body>
      <div class="container">
        <h1>To-Do List</h1>
        <form action="#">
          <input type="text" class="form-control" placeholder="Add new task...">
          <button id="add-task-button" type="submit">Add Task</button>
        </form>
        <ul id="task-list" class="list-group">
          <!-- The tasks will be inserted here -->
        </ul>
      </div>
      <script>
        var addTaskButton = document.getElementById("add-task-button");
        addTaskButton.addEventListener("click", function() {
          var inputValue = document.getElementsByClassName("form-control")[0].value;
          if (inputValue !== "") {
            var liElement = document.createElement("li");
            liElement.innerHTML = inputValue;
            document.getElementById("task-list").insertBefore(liElement, document.getElementById("task-list").firstChild);
            document.getElementsByClassName("form-control")[0].value = "";
          }
        });
        
        // Mark task as completed
        var taskList = document.getElementById("task-list");
        taskList.addEventListener("click", function(event) {
          if (event.target.tagName === "LI") {
            event.target.classList.toggle("completed");
          }
        });
      </script>
    </body>
    </html>
    

    I hope this is helpful!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.