in these lines of code #result is a link to a div's id for a different HTML file than I'm using in a function before in my JS file.
Anyone knows how can I make it work and this JS function will append to an empty div id="result" I have in different HTML?
function getDetails() {
window.close();
var parentEl = $(this).parents('[data-book-id]'); // <-- index.html
var bookAuthor = parentEl.find('[data-book-author-input]').val(); // <-- index.html
var bookTitle = parentEl.find('[data-book-title-input]').val(); // <-- index.html
var bookYearOfPublication = parentEl.find('[data-book-year-input]').val(); // <-- index.html
var search = bookAuthor + bookTitle + bookYearOfPublication;
if(search == "")
{
alert("Please enter something in the field");
}
else{
window.open('http://localhost:8081/details');
var url = "";
var img = "";
var title = "";
var author = "";
$.get("https://www.googleapis.com/books/v1/volumes?q=" + search, function(response){
for(i=0;i<response.items.length;i++)
{
title=$('<h5 class="center-align white-text">' + response.items[i].volumeInfo.title + '</h5>');
author=$('<h5 class="center-align white-text"> By:' + response.items[i].volumeInfo.authors + '</h5>');
img = $('<img class="aligning card z-depth-5" id="dynamic"><br><a href=' + response.items[i].volumeInfo.infoLink + '><button id="imagebutton" class="btn red aligning">Read More</button></a>');
url= response.items[i].volumeInfo.imageLinks.thumbnail;
img.attr('src', url);
title.appendTo('#result'); // <-- details.html
author.appendTo('#result'); // <-- details.html
img.appendTo('#result'); // <-- details.html
}
});
}
return false;
}
With the local storage:
function getDetails() {
var parentEl = $(this).parents('[data-book-id]'); var book = parentEl.find('[data-book-author-input]').val() + parentEl.find('[data-book-title-input]').val() + parentEl.find('[data-book-year-input]').val(); window.open('localhost:8081/details'); window.localStorage.setItem('book', JSON.stringify(book)); } // <-- index.html
booksContainer.on('click', '[data-book-details-button]', getDetails); // <-- index.html
function setDetails() {
var search = window.localStorage.getItem('book');
if(search == "")
{
alert("The value from API is null");
}
else{
var url = "";
var img = "";
var title = "";
var author = "";
$.get("https://www.googleapis.com/books/v1/volumes?q=" + search, function(response){
for(i=0;i<response.items.length;i++)
{
title=$('<h5 class="center-align white-text">' + response.items[i].volumeInfo.title + '</h5>');
author=$('<h5 class="center-align white-text"> By:' + response.items[i].volumeInfo.authors + '</h5>');
img = $('<img class="aligning card z-depth-5" id="dynamic"><br><a href=' + response.items[i].volumeInfo.infoLink + '><button id="imagebutton" class="btn red aligning">Read More</button></a>');
url= response.items[i].volumeInfo.imageLinks.thumbnail;
img.attr('src', url);
title.appendTo('#result');
author.appendTo('#result');
img.appendTo('#result');
window.localStorage.clear();
}
});
}
return false;
} // <-- details.html
$('[search-details-button]').on('click', setDetails); // <-- details.html
.jsfile, instead of writing directly the javascript code in the html file)