CREATE: Add a Task
The Create operation allows the user to add a new task.
Create a tag to enter the javascript within the HTML file
html
<script>
</script>
<script> tag add the getTaskText() function to retrieve the user input text from the <input> tag
javascript
function getTaskText(){
const input = document.getElementById('taskInput');
const text = input.value.trim();
console.log("The input value is: " + input.value);
return text;
}
addTask() to parse the taskText as a task item
javascript
function addTask(){
const taskText = getTaskText();
addTaskItem(taskText);
}
addTaskItem(taskTexT) to render the task item on the taskList
javascript
function addTaskItem(taskText){
const li = document.createElement('li');
const taskSpan = document.createElement('span');
taskSpan.textContent = taskText;
li.appendChild(taskSpan);
document.getElementById('taskList').appendChild(li);
}
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Todolist Applicaiton</title>
</head>
<body>
<h1>Todo List</h1>
<input type="text"
id="taskInput"
placeholder="Add a task"
/>
<button onclick="addTask()">Add Task</button>
<ul id="taskList">Your tasks go here ...</ul>
<script>
function getTaskText(){
const input = document.getElementById('taskInput');
const text = input.value.trim();
console.log("The input value is: " + input.value);
return text;
}
function addTask(){
const taskText = getTaskText();
addTaskItem(taskText);
}
function addTaskItem(taskText){
const li = document.createElement('li');
const taskSpan = document.createElement('span');
taskSpan.textContent = taskText;
li.appendChild(taskSpan);
}
</script>
</body>
</html>
Testing the initial functionalities
Testing Checklist
text
[ ] Add a task
[ ] Add multiple tasks
Sign in to save your progress permanently.
Progress is saved in your browser session
Step 3 of 7