0

am a newbie and am stuck with this for a week now, enter image description here when I click the button to save the book information in session storage and post the book information in an other div, I get this result:the div is written twice, once with the css and the other without it, this my code:( for the whole code git@github.com:yaminecy/html.git)

` 
 //icons buttons
              let bookmark = '<a id="' + items[i].id + '" data-id="' + items[i].id + '" data-title="' + items[i].volumeInfo.title + '" data-description="' + items[i].volumeInfo.description + '" data-author ="' + items[i].volumeInfo.authors + '" data-image ="' + items[i].volumeInfo.imageLinks.smallThumbnail + '"><i id="bookmark" class="fas fa-bookmark fa-3x"></i></a>';
              let trashicon = '<a class="trash" id="' + items[i].id + '" onclick="return this.parentNode.remove();"><i id="icon2" class="fas fa-trash fa-3x"></i></a>';


              //add elements to the book.
              const livre = document.createElement("div");
              livre.innerHTML = '<div id= "livre">' + bookmark + title + id + author + selectdescription + image  + '</div>';
             capture.appendChild(livre);

              let bookmarkbutton = document.getElementById(items[i].id);
              //click bookmark button 
              bookmarkbutton.addEventListener('click', function () {
                var book = '{"id": "' + this.dataset.id + '", "name": "' + this.dataset.title + '", "description" : "' + this.dataset.description + '", "author" : "' + this.dataset.author + '", "image": "' + this.dataset.image + '"}';
                favori(book);
              });
                //create a function for the action click on the button "bookmark"
                function favori(item) {
                  console.log(item);
                  let pochList = [];
                  if (sessionStorage.getItem("maPochList") !== null) {
                    pochList = JSON.parse(sessionStorage.getItem("maPochList"));
                  }
  
                  if (pochList.includes(item)) {
                    alert("Vous ne pouvez pas ajouter deux fois le même livre!")
                  }
                  else {
                    pochList.push(item);
                    sessionStorage.setItem("maPochList", JSON.stringify(pochList));
                    item = JSON.parse(item);
                    favorite_books.append(setFavori(item)); 
                  }
                }

              // create a function that returns a div with information of favorite books
              let bookdiv = document.createElement("div");
              favorite_books.appendChild(bookdiv);
            
              function setFavori() {
               return bookdiv.innerHTML= '<div id= "livre">' + trashicon + title + id + author + selectdescription + image + '</div>';;
                }

              }
          }
        }
     ))
    }
  //Show Favorites
  let pochList = [];
  if (sessionStorage.getItem("maPochList") !== null) {
    pochList = JSON.parse(sessionStorage.getItem("maPochList"));
  }

  for (var k in pochList) {
    var item = JSON.parse(pochList[k]);
    bookdiv.append(setFavori(item));
  }
}    ` 
Yamine
  • 13
  • 3
  • Why are you doing `bookdiv.append(setFavori(item));` instead of just `setFavori(item);`? Why are you returning an assignment? Do you know what `element.append("
    ")` does? Read the [documentation](//developer.mozilla.org/docs/Web/API/ParentNode/append) and see [Append html element to the DOM instead of string of html](/q/53746340/4642212).
    – Sebastian Simon Jul 22 '21 at 17:01
  • 1
    `setFavori(item)`, however your setFavori function doesn't accept a parameter - – James Jul 22 '21 at 17:02
  • wow that was faaaast , your answer is great, it went straight to the point, thanks a lot budy, this solved the probelem, and thanks for the links to the documentation – Yamine Jul 22 '21 at 17:06
  • The posted JavaScript is not valid, there are lots of unmatched `}` – Barmar Jul 22 '21 at 17:06

0 Answers0