Grok 4.1 Fast· xai
Frontier Reasoning Traces
Write an event listener that dynamically adds items to a to-do list HTML ul/li and…
- User
Write an event listener that dynamically adds items to a to-do list HTML ul/li and persists the list via localStorage.
- Assistant
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Persistent To-Do List</title> <style> body { font-family: Arial, sans-serif; max-width: 500px; margin: 50px auto; } input { width: 70%; padding: 10px; } button { padding: 10px 20px; } ul { list-style: none; padding: 0; } li { padding: 10px; border-bottom: 1px solid #ddd; } </style> </head> <body> <h1>To-Do List</h1> <input type="text" id="todoInput" placeholder="Enter a new task..." /> <button id="addBtn">Add Task</button> <ul id="todoList"></ul> <script> // Load todos from localStorage on page load let todos = JSON.parse(localStorage.getItem('todos')) || []; const todoInput = document.getElementById('todoInput'); const addBtn = document.getElementById('addBtn'); const todoList = document.getElementById('todoList'); // Function to render the list function renderTodos() { todoList.innerHTML = ''; todos.forEach((todo, index) => { const li = document.createElement('li'); li.textContent = todo; // Optional: Add a delete button const deleteBtn = document.createElement('button'); deleteBtn.textContent = 'Delete'; deleteBtn.onclick = () => { todos.splice(index, 1); localStorage.setItem('todos', JSON.stringify(todos)); renderTodos(); }; li.appendChild(deleteBtn); todoList.appendChild(li); }); } // Event listener for Add button addBtn.addEventListener('click', () => { const value = todoInput.value.trim(); if (value) { todos.push(value); localStorage.setItem('todos', JSON.stringify(todos)); renderTodos(); todoInput.value = ''; } }); // Event listener for Enter key in input todoInput.addEventListener('keypress', (e) => { if (e.key === 'Enter') { addBtn.click(); // Trigger the add button } }); // Initial render renderTodos(); </script> </body> </html>This is a complete, self-contained example. The key event listeners are:
addBtn.addEventListener('click', ...): Adds a new item, persists tolocalStorage, re-renders the list.todoInput.addEventListener('keypress', ...): Allows adding via Enter key.
The list persists across page reloads/sessions via
localStorage. I've included a simple delete button per item for usability (optional but enhances the to-do list). Copy-paste into an HTML file to test!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.