0

So in this following code I tried to clear the after I added some Text (in this case they represent ingredients for a shopping list) to the div. Im fairly new to Javascript and dont have that much expirience yet so any help would be much appreciated!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button id="apple" onclick="apple()">Apple</button>
    <button id="tomato" onclick="tomato()">Tomato</button>
    <button id="eggs" onclick="eggs()">Eggs</button>
    <button id="clear" onclick="clear()">Clear</button>
    <div id="cart"></div>

    <script>

        const cart = document.getElementById('cart');

        function apple(){
            let div = document.createElement('div');
            let foodItem = document.getElementById('apple');
            div.textContent = 'Apple';
            cart.appendChild(div);
        }
        function tomato(){
            let divTwo = document.createElement('div');
            let foodItem = document.getElementById('tomato');
            divTwo.textContent = 'Tomatos';
            cart.appendChild(divTwo);
        }
        function eggs(){
            let divThree = document.createElement('div');
            let foodItem = document.getElementById('eggs');
            divThree.textContent = 'Eggs';
            cart.appendChild(divThree);
        }
        function clear(){
            document.getElementById('cart').innerHTML = '';
        }
    </script>
</body>
</html>
  • `clear` is shadowed by [`Document.prototype.clear`](//developer.mozilla.org/en/docs/Web/API/Document/clear). Inline event handlers like `onclick` are [bad practice](/q/11737873/4642212). They’re an [obsolete, cumbersome, and unintuitive](/a/43459991/4642212) way to listen for events. Always [use `addEventListener`](//developer.mozilla.org/en/docs/Learn/JavaScript/Building_blocks/Events#inline_event_handlers_%E2%80%94_dont_use_these) instead. This is one of the many reasons why. – Sebastian Simon Dec 19 '22 at 01:42
  • but I never used addEventListener and right now im following a project based tutorial on youtube. So I cannot implement it into my code for now. I still dont understand why my code doesnt work – Bobanius Jr. Dec 19 '22 at 01:47
  • Don’t follow random outdated YouTube tutorials with zero community curation. Read the official [JS docs](//developer.mozilla.org/en/docs/Web/JavaScript/Reference), and [JS tutorials](//developer.mozilla.org/en/docs/Web/JavaScript/Guide). – Sebastian Simon Dec 19 '22 at 01:48
  • Thanks for the help, it worked because I changed the name of the function, just like you said. The thing with this youtube tutorial is that it's dumbed down for beginners but the more you go on into the course, the more he (the youtuber explaining it) will make you aware of those kind of things. I have learned the basics of CSS and HTML from him, where at the beginning he also used suboptimall "code" – Bobanius Jr. Dec 19 '22 at 01:59

0 Answers0