There are multiple options for you to try.
You can select all <p> tags from the <section> element with querySelectorAll() and loop through each paragraph and create an input for it. Though you would still need a <form> element to wrap around the inputs to capture the data on submit.
And keep in mind, inputs need the name attribute to know which field belongs with each value. You could use the class attribute value as the name attribute value of each input you loop over.
else if(e.target && e.target.textContent === 'Update') {
const section = e.target.parentNode;
const paragraphs = section.querySelectorAll('p');
for (const paragraph of paragraphs) {
const name = paragraph.className;
const input = document.createElement('input');
input.name = name;
input.type = 'text';
input.value = paragraph.textContent;
section.replaceChild(input, paragraph);
}
}
Alternatively you could create multiple functions for each template that you use. Like a display template and an edit template where you switch between the two templates whenever based on if you need to display or edit your values.
const createDisplayTemplate = movie => `
<p class="id"> ${movie.id}</p>
<p class="title"> Title: ${movie.title}</p>
<p class="runtime">Runtime: ${movie.runtime}</p>
<p class="release-date">Release date: ${movie.releaseDate}</p>
<button class="delete-btn">Delete</button>
<button class="update-btn">Update</button>
`;
const createEditTemplate = movie => `
<form>
<input type="text" name="id" value="${movie.id}">
<input type="text" name="title" value="${movie.title}">
<input type="text" name="run-time" value="${movie.runtime}">
<input type="text" name="release-date" value="${movie.releaseDate}">
<button class="save-btn">Save</button>
</form>
`;
const displayTemplate = createDisplayTemplate(movie);
const editTemplate = createEditTemplate(movie);
The next one is maybe the easiest method. You could insert both the content and the inputs, and toggle between them by adding or removing classes, whenever you need one of them. Like the example below.
const section = document.querySelector('section');
const display = section.querySelector('.js-display');
const edit = section.querySelector('.js-edit');
section.addEventListener('click', event => {
const { target } = event;
if (target.classList.contains('update-btn')) {
display.classList.add('hide');
edit.classList.remove('hide');
} else if (target.classList.contains('save-btn')) {
display.classList.remove('hide');
edit.classList.add('hide');
}
});
edit.addEventListener('submit', event => {
event.preventDefault();
});
.hide {
display: none;
}
<section>
<div class="js-display">
<p class="id">1</p>
<p class="title"> Title: The Matrix</p>
<p class="runtime">Runtime: 2h 16m</p>
<p class="release-date">Release date: 1999</p>
<button class="delete-btn">Delete</button>
<button class="update-btn">Update</button>
</div>
<form class="js-edit hide">
<input type="text" name="id" value="1">
<input type="text" name="title" value="The Matrix">
<input type="text" name="run-time" value="2h 16m">
<input type="text" name="release-date" value="1999">
<button class="save-btn">Save</button>
</form>
</section>
Or as a last resort: change all of your <p> tags to <input> tags from the start. Disable the input tags so they can't be edited right away.
On clicking the update button loop over the inputs and change the disabled property to either true or false.
The example below shows how this should look.
The only concern here is accessibility. Inputs are not text elements and will be read differently by screen readers and other tools. So if that is a concern to you then use one of the options above.
const section = document.querySelector('section');
const form = document.querySelector('.js-edit');
section.addEventListener('click', event => {
const { target } = event;
if (target.classList.contains('update-btn')) {
for (const input of form.elements) {
if (input.type === 'text') {
input.disabled = !input.disabled;
}
}
}
});
form.addEventListener('submit', event => {
event.preventDefault();
});
form {
display: flex;
flex-direction: column;
}
input[type="text"] {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
margin: 10px 0;
padding: 5px;
background: #f7f7f7;
}
input[type="text"]:disabled {
border: 0;
background: none;
}
<section>
<form class="js-edit" >
<input type="text" name="id" value="1" disabled>
<input type="text" name="title" value="The Matrix" disabled>
<input type="text" name="run-time" value="2h 16m" disabled>
<input type="text" name="release-date" value="1999" disabled>
<button class="update-btn">Update</button>
<button class="delete-btn">Delete</button>
</form>
</section>
display: noneto hide the one that isn't needed at first, then use JS to change the CSS after the button click. Maybe I'm weird.