Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How do I make my favorites button show a heart emoji above my recipeTitle with LocalStorage when it is clicked?

Hi guys! :)

I've been stuck for 2 hours with this problem in JavaScript. I've tried to use event listeners but it didn't work out. I want to add an 'add to favorites' button 'šŸ˜' with LocalStorage in JavaScript. When I click on the 'Add to favorites button' I want to show 'šŸ˜' below the recipeTitle for the specific recipe-wrapper id item that is clicked at the same I want to modify the CSS background-color: pink; in JavaScript. I also want to undo the favorite by removing 'šŸ˜' below the recipeTitle, also the background-color: pink should be removed.

// Global variables
let containerForInfoParent = document.querySelectorAll('.container');
let recipeButton = document.getElementsByClassName('show-recipe-button');
let infoWrapper = document.querySelector('#info-wrapper');

// init
window.addEventListener("load", init);

function init() {
    getRecipes();
    for (let i = 0; i < recipeButton.length; i++) {
        recipeButton[i].addEventListener('click', getDetails(`${i}`));
        console.log(recipeButton);
    }
}

function getDetails(e) {
    let id = e.target.id.slice(12);
    console.log(id)
    fetch (`http://localhost/magazine/webservice?id=${id}`)
        .then(response => response.json()

        )

        .then(data => {
            let recipeDetails = document.querySelector('#recipe-details');
            recipeDetails.innerText = data.recipe;
            console.log(recipeDetails);

            let recipeTags = document.querySelector('#recipe-tags');
            recipeTags.innerText = data.tags.toString();
            console.log(recipeTags);



            // test in console console.log(recipeDetails);
        });
}

/**
 * Get recipes: Returns a collection of objects in JSON-format
 */
function getRecipes() {
    fetch ("http://localhost/magazine/webservice")
        .then(response => response.json()

        )

        .then(data => {
            for (let recipe of data) {
                let recipeObjects = recipe;
                createRecipe(recipe.id, recipe.name, recipe.kitchen, recipe.image);
            }
        });
}

function createRecipe(id, name, kitchen, image) {
    let rowDiv = document.createElement('div');
    // rowDiv.classList.add('row');

    let recipeDiv = document.createElement('div'); // <div> in memory
    recipeDiv.classList.add('col-sm');
    recipeDiv.classList.add('card');

    let recipeTitle = document.createElement('h1');
    recipeTitle.innerText = name;
    recipeDiv.appendChild(recipeTitle);

    let recipeImage = document.createElement('img');
    recipeImage.src = `./images/${image}.jpg`;
    recipeImage.classList.add('recipe-image');
    recipeDiv.appendChild(recipeImage);

    // Voeg toe als favoriet knop
    let recipeButtonOne = document.createElement('button');
    recipeButtonOne.classList.add('btn');
    recipeButtonOne.classList.add('btn-warning');
    recipeButtonOne.innerText = 'Add to favorites';
    recipeButtonOne.setAttribute('id', `add-to-favorites-${id}`);
    recipeDiv.appendChild(recipeButtonOne);

    let recipeButtonTwo = document.createElement('button');
    recipeButtonTwo.addEventListener('click', getDetails); // Event listener
    recipeButtonTwo.classList.add('btn');
    recipeButtonTwo.classList.add('btn-warning');
    recipeButtonTwo.innerText = 'Show recipe';
    recipeButtonTwo.setAttribute('id', `show-recipe-${id}`);
    recipeDiv.appendChild(recipeButtonTwo);

    rowDiv.appendChild(recipeDiv);
    console.log(rowDiv);

    let recipeWrapper = document.querySelector('.recipe-wrapper');
    recipeWrapper.appendChild(rowDiv);
}
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Food Magazine! - Kimono</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="css/style.css"/>
</head>
<body>
<!-- TODO -->

<div class="top">
    <h1 class="food-magazine-title">Food Magazine!</h1>
</div>


<div class="main-wrapper">

    <div class="recipe-wrapper">

    </div>

    <div id="info-wrapper">
        <h2>Recipe</h2>
            <p id="recipe-details">

            </p>
        <h2>Tags</h2>
            <p id="recipe-tags">

            </p>
    </div>

</div>

<div class="bottom">
    <p class="bottom-copyright">Ā© 2021 Created by Kimono.</p>
</div>
<script type="text/javascript" src="js/main.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
</body>
</html>