TIA for any help. I am trying to create a new field when I click a button. This is for a to-do list project. I tried to make a function that would create the new field. Then after creating the function I put it into an .eventListener when the button was clicked. I am not sure where to go from here.
Here is my code
**Javascript **
const button = document.querySelector('.createnew')
const field = document.querySelector('.field');
function newField() {
const inputElement = document.createElement("input.value");
inputElement.setAttribute("type", "text");
return field.appendChild(inputElement);
}
button.addEventListener('click', newField);
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>To-Do</title>
</head>
<body>
<h1 class="header">To-Do List</h1>
<div class="container">
<div class="form">
<input class="field" type="text" placeholder="Input Text">
<input class="check" type="checkbox">
</div>
<button class="createnew">Add New Field</button>
</div>
<script src="index.js"></script>
</body>
</html>
If I understood your task properly, it can be achieved as shown below.
In your initial code, you've tried adding a new input as a child to the existing input, which doesn't seem right because you need to add a new input into the form instead.
Please let me know if this helps.