$( document ).ready(function() { let items = []; let itemsRaw = []; $.getJSON('/api/books', function(data) { //let items = []; itemsRaw = data; $.each(data, function(i, val) { items.push('
...and '+ (data.length - 15)+' more!
'); } $(''+data+'
Refresh the page
'); } }); }); $('#bookDetail').on('click','button.addComment',function() { let newComment = $('#commentToAdd').val(); $.ajax({ url: '/api/books/'+this.id, type: 'post', dataType: 'json', data: $('#newCommentForm').serialize(), success: function(data) { comments.unshift(newComment); //adds new comment to top of list $('#detailComments').html(comments.join('')); } }); }); $('#newBook').click(function() { $.ajax({ url: '/api/books', type: 'post', dataType: 'json', data: $('#newBookForm').serialize(), success: function(data) { //update list } }); }); $('#deleteAllBooks').click(function() { $.ajax({ url: '/api/books', type: 'delete', dataType: 'json', data: $('#newBookForm').serialize(), success: function(data) { //update list } }); }); });