0

I'm using eclipse to write an html page that uses a JavaScript function that connects to the google BookAPI. I based mine off of the one I found here (https://codepen.io/mkokes/pen/KqvZNY). I changed it slightly so that instead of using an ISBN number it uses the name of the book and the author to find the cover page.

I get a result on the book but its the wrong one (always the same one so I assume its a problem when I call the script).

My code to call and use the script looks like this:

<section class="styles" data-title="Animal Farm" data-author="George Orwell">
  
  <img src="" alt="" class="thumbnail" />  
  
  <header>
    <h3 class="title"></h3>
    <h4 class="author"></h4>
  </header>
  
</section>

The styles is the css that I use to format the page. Is that the right thing to put there or am I completely wrong?

My JavaScript is as follows:

var title = $('.book').data('title');
var author = $('.book').data('author');

$.ajax({
  dataType: 'json',
  url: 'https://www.googleapis.com/books/v1/volumes?q=' + title + '+inauthor:' + author + '&orderBy=relevance&maxResults=1',
  success: handleResponse
});

function handleResponse( response ) {
  $.each( response.items, function( i, item ) {
    
    var title    = item.volumeInfo.title,
        author   = item.volumeInfo.authors[0],        
        thumb    = item.volumeInfo.imageLinks.thumbnail;
    
    $('.title').text( title );
    $('.author').text( author );
    $('.thumbnail').attr('src', thumb);
  });
}

If anyone has any suggestions I'd really appreciate it. Would be great to finally get this working.

nitind
  • 19,089
  • 4
  • 34
  • 43
  • in your JS you are looking for an element with class book to get its data-attribute but you have no elements with class book in your html – Lelio Faieta Apr 12 '23 at 14:44

2 Answers2

0

I think you are a bit mixed up on CSS classes. Think of it this way:

The id attribute in HTML is a unique identifier, no other element on the page can use that same ID. There's only one. Exmaple <div id="navigation">

The class attribute in HTML can contain multiple classes. A "class" is basically just another identifier, but the page can contain multiples of these. A good example of this for your code would be .book however it looks as if your HTML doesn't actually use that class anywhere. Perhaps this should be: <section class='book'

You currently have class="styles" on that, and I think that's where you're mixed up, unless you actually have a CSS selector for .styles somewhere, and if you did you should rename that to something that's more descriptive.

Also, both the id and class attributes can be used in CSS and in JS for selecting elements.

I've re-written your code a bit and added some styles for an example.

var books = $('.book'); //This will select all of the .book elements and return an array

//Now we can loop through each selected .book element: https://api.jquery.com/each/
books.each(function(){
  var thisBook = $(this);
  var title = thisBook.data('title');
  var author = thisBook.data('author');

  $.ajax({
    dataType: 'json',
    url: 'https://www.googleapis.com/books/v1/volumes?q=' + title + '+inauthor:' + author + '&orderBy=relevance&maxResults=1',
    success: function(response){
      handleResponse(thisBook, response);
    }
  });
});

function handleResponse( thisBook, response ) {
  $.each( response.items, function( i, item ) {
    
    var title    = item.volumeInfo.title,
        author   = item.volumeInfo.authors[0],        
        thumb    = item.volumeInfo.imageLinks.thumbnail;
    
    thisBook.find('.title').text( title );
    thisBook.find('.author').text( author );
    thisBook.find('.thumbnail').attr('src', thumb);
  });
}
.book {
  border: 1px solid #333;
  padding: 5px;
  margin-bottom: 10px;
}

.book .thumbnail {
  max-height: 200px;
  margin-right: 10px;
  float: left;
}

.book header {
  background: #CCC;
  
}

.book .title{
  margin-top: 0;
}

.book author {

}

.clear {
  clear: both;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section class="book" data-title="Animal Farm" data-author="George Orwell">
  
  <img src="" alt="" class="thumbnail" />  
  
  <header>
    <h3 class="title"></h3>
    <h4 class="author"></h4>
  </header>
  
  <div class='clear'></div>
</section>

<section class="book" data-title="The Great Gatsby" data-author="F. Scott Fitzgerald">
  
  <img src="" alt="" class="thumbnail" />  
  
  <header>
    <h3 class="title"></h3>
    <h4 class="author"></h4>
  </header>
  
  <div class='clear'></div>
</section>

<section class="book" data-title="Moby Dick" data-author="Herman Melville">
  
  <img src="" alt="" class="thumbnail" />  
  
  <header>
    <h3 class="title"></h3>
    <h4 class="author"></h4>
  </header>
  
  <div class='clear'></div>
</section>
Chris Barr
  • 29,851
  • 23
  • 95
  • 135
  • In the code I based mine off, class='media book showcase' but unless I've missed something I cant find what that's actually referencing. I tried your suggestion of putting book in the class attribute but that didn't seem to change anything. – Icarus101 Apr 12 '23 at 14:36
  • @Icarus101, look for `.media`, `.book`, `.showcase` in the original CSS. You didn't include your CSS, but unless you have defined styles for `.styles{your styles here}`, it won't do anything. – mykaf Apr 12 '23 at 14:40
  • @FiniteLooper Hi, first off, thanks for the answer. Didnt think anyone would literally rewrite my code. But when I try the code in my program I dont end up with what the code snippet does. For some reason I only get the 3 sections with nothing in them. Any idea what could be causing this? – Icarus101 Apr 13 '23 at 13:25
  • It’s hard to say just based on your description. Do you have errors in your console? If you are running this locally, like just files on your computer and not in a local dev server, that might be the issue. – Chris Barr Apr 13 '23 at 20:10
0
<section class="styles" data-title="Animal Farm" data-author="George Orwell">

should be

<section class="styles book" data-title="Animal Farm" data-author="George Orwell">

so that you can then look for the data attributes in your js with:

var title = $('.book').data('title');
var author = $('.book').data('author');

I don't know what you use styles class for but you can keep it even in case it is unused.

When you do $('.book'). in jquery you are looking for all the items on your page with the class book. When you do $('#book'). you look for the objcet with the id book (ids are unique in a page). In your html you will probably have many different classes and ids that are not related (or may be not related) to any css rule but used only for js purposes

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74