I have this javascript code that aims to display data into my html table. I am now stuck as to how should i display my data into my html. Here is my code snippet:
document.getElementById('save-book').addEventListener('click', (e) =>{
books = '';
if(document.getElementById('books').value){
books = document.getElementById('books').value;
}
book_name = document.getElementById('book_name').value;
description = document.getElementById('description').value;
if(book_name && description){
books = books.replace("[","").replace("]","");
let book_object = {"id": 0, "book_name": book_name, "description": description};
book_object = JSON.stringify(book_object);
books = "["+books+", "+book_object+"]";
for (var i = JSON.parse(books).length - 1; i >= 0; i--) {
console.log(JSON.parse(books)[i].id);
//replace the current rows from a table by this object books.
}
}
document.getElementById('book_name').value = '';
document.getElementById('description').value = '';
});
where my books
tag holds a data that looks like
[{"id": 1, "book_name":"Harry Potter","description":"Harry Potter is a series of fantasy novels written by British author J. K. Rowling"},{"id" : 2, "book_name":"Wizard of Oz","description":"The description here"}]
and my table looks like this:
<table id="book_table" class="display">
<thead>
<tr>
<th class="hide">Id</th>
<th>Book Name</th>
<th>Description</th>
<th class="right-align">Action</th>
</tr>
</thead>
<tbody>
@if($books->count() > 0)
@foreach ($books as $book)
<tr>
<td class="hide">{{ $book->id }}</td>
<td>{{ $book->book_name }}</td>
<td>{{ $book->description }}</td>
<td></td>
</tr>
@endforeach
@endif
</tbody>
</table>
How can i replace and show my books array into the above table?