const firstTodo = document.querySelector("h4");
console.log(firstTodo.innerHTML);
const secondTodo = document.querySelectorAll("h4")[1];
console.log(secondTodo.innerHTML);
const firstTodo = document.querySelector("h4");
firstTodo.innerHTML = "Dont' take class";
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>replit</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Todo list</h1>
<div>
<div id="todo-1">
<h4>1. Take class</h4>
<button onclick="deleteTodo(1)">delete</button>
</div>
<div id="todo-2">
<h4>2. Go out to eat</h4>
<button onclick="deleteTodo(2)">delete</button>
</div>
</div>
<div>
<input type="text"></input>
<button>Add Todo</button>
</div>
</body>
<script>
function deleteTodo(index) {
const element = document.getElementById("todo-" + index);
element.parentNode.removeChild(element);
}
</script>
</html>
Steps -
- Get the current text inside the input element
- Create a new
div
element - Add the text
from
step 1 to thediv
element - Append the
div
to the todos list
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>replit</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Todo list</h1>
<div id="todos">
<div id="todo-1">
<h4>1. Take class</h4>
<button onclick="deleteTodo(1)">delete</button>
</div>
<div id="todo-2">
<h4>2. Go out to eat</h4>
<button onclick="deleteTodo(2)">delete</button>
</div>
</div>
<div>
<input id="inp" type="text"></input>
<button onclick="addTodo()">Add Todo</button>
</div>
</body>
<script>
function addTodo() {
const inputEl = document.getElementById("inp");
const textNode = document.createElement("div");
textNode.innerHTML = inputEl.value;
const parentEl = document.getElementById("todos");
parentEl.appendChild(textNode);
}
</script>
</html>