Proponuje użyć, closest(...) i getAttribute(...) (dataset) Codepen
<table>
<!-- ${book.isbn} -->
<tr id="book-list" data-isbn="978-83-246-8920-0">
<!-- ${book.title} -->
<td>C++. Przewodnik dla początkujących</td>
<!-- ${book.author} -->
<td>Alex Allain</td>
<!-- ${book.isbn} -->
<td>978-83-246-8920-0</td>
<!-- ${book.pages} -->
<td>400</td>
<td>
<a href='#' class='btn btn-success done'><b>√</b></a>
<a href='#' class='btn btn-light edit'><i class="fas fa-pen edit"> edit</i></a>
<a href='#' class='btn btn-danger delete'>X</a>
</td>
</tr>
<!-- ${book.isbn} -->
<tr id="book-list" data-isbn="978-83-246-4886-3">
<!-- ${book.title} -->
<td>HTML5. Podręcznik programisty</td>
<!-- ${book.author} -->
<td>Chuck Hudson</td>
<!-- ${book.isbn} -->
<td>978-83-246-4886-3</td>
<!-- ${book.pages} -->
<td>384</td>
<td>
<a href='#' class='btn btn-success done'><b>√</b></a>
<a href='#' class='btn btn-light edit'><i class="fas fa-pen edit"> edit</i></a>
<a href='#' class='btn btn-danger delete'>X</a>
</td>
</tr>
</table>
<div id="edit-box" class="hide">
<input type="text" id="title">
<input type="text" id="author">
<input type="text" id="number-of-pages">
<input type="button" id="submit">
</div>
table {
width: 95vw;
}
#book-list td {
font-weight: 500; /* <td style='font-weight: 500;'> */
}
td {
padding: 0.25em;
}
input[type="text"] {
display: block;
margin: 0.25em;
width: 20em;
}
input[type="button"] {
display: block;
margin: 0.5em 0.25em;
width: 5em;
}
a.btn {
font-size: 0.8em;
padding-right: 0.5em;
text-decoration: none;
}
.hide {
display: none;
}
const edit_box = document.querySelector('#edit-box');
const submit = document.querySelector('#submit');
let isEdited = false;
[...document.querySelectorAll('#book-list')].forEach(book => {
book.addEventListener('click', (e) => {
const el = e.target;
if (el.classList.contains('edit')) {
if (isEdited === false) {
isEdited = true;
const isbn = el.closest('tr#book-list').getAttribute('data-isbn');
//console.log(isbn);
editBook(isbn);
}
}
});
});
submit.addEventListener('click', (e) => {
edit_box.classList.add('hide');
isEdited = false;
// Twój kod zapis edytowanych danych
});
function editBook(isbn) {
edit_box.classList.remove('hide');
submit.style.backgroundColor = 'orange';
submit.value = 'Save';
/*
const books = JSON.parse(localStorage.getItem('books'));
books.forEach( (book) => {
if (book.isbn === isbn) {
document.querySelector('#title').value = book.title;
document.querySelector('#author').value = book.author;
document.querySelector('#number-of-pages').value = book.pages;
}
});
*/
document.querySelector('#title').value = isbn; // dla demonstarcji
}