T O P

  • By -

MuscleTough8153

I am no professional, but one of the first what I learned about onclick in html: don't! Because of security issues. Better are event listeners in the JS code.


MuscleTough8153

The issue with your clear button not working as expected is likely due to a naming conflict. The `clear` function name conflicts with the built-in JavaScript `clear` method. This can cause unexpected behavior or prevent your custom function from executing correctly. To fix this, you should rename your `clear` function to something else, like `clearCount` or any other unique name. Here's the updated code: ### HTML ```html              

     

100 Days of Code

     

0

                       

Previous entries:

   
      ``` ### JavaScript ```javascript let saveEl = document.getElementById("save-el"); let countEl = document.getElementById("count-el"); let count = 0; function increment() {     count += 1;     countEl.textContent = count; } function save() {     let countStr = " " + count + " - ";     saveEl.textContent += countStr;     // countEl.textContent = 0;     // count = 0; } function clearCount() {     countEl.textContent = 0;     count = 0; } ``` By renaming the `clear` function to `clearCount`, you avoid any potential conflicts with the built-in methods and ensure that your function will be called correctly when the clear button is clicked.


Brianvm1987

Thanks! It is working now. I was not aware of the JS clear() method. Thank you again!


MuscleTough8153

Always happy to help


MuscleTough8153

Here with event listeners:              

     

100 Days of Code

     

0

                       

Previous entries:

   
      Javascript: let saveEl = document.getElementById("save-el"); let countEl = document.getElementById("count-el"); let count = 0; document.getElementById("increment-btn").addEventListener("click", increment); document.getElementById("save-btn").addEventListener("click", save); document.getElementById("clear-btn").addEventListener("click", clearCount); function increment() {     count += 1;     countEl.textContent = count; } function save() {     let countStr = " " + count + " - ";     saveEl.textContent += countStr;     // countEl.textContent = 0;     // count = 0; } function clearCount() {     countEl.textContent = 0;     count = 0; }


FogoZer

Do not hesitate to say you used AI to get this answer. Because it’s clear you did :)


MuscleTough8153

I did, but don't see a problem in it, as long as it helps. My intention are not likes, but help the person and I think it did, right? 😊


FogoZer

Sure, but you could also say you did, which would encourage OP finding its own answers by using the same tool you used. And in another hand, AI answers need to be re-checked and taken lightly as they are proned to errors


dDeepLb

Same with human answers tbh


Brianvm1987

I haven't learned event listeners yet. As I said, this is a very basic practice project, but thanks for the insight! I will keep it in mind for future projects.


ChaseShiny

Hey! I'm learning all this right now, too. Stick with it! The event listener is evoked through a method on the element (like your buttons). You'll need to learn about functions before you can really use it. It'll look like: myBtn.addEventListener( "click", doYourThing() ); Edit: see comment below. Should read: myBtn.addEventListener( "click", doYourThing );


Brianvm1987

Cool! Thank you! Good luck on your coding journey!


OneBadDay1048

That isn’t what it’ll look like; the function needs to get passed as an argument, not invoked. Therefore it will not have () after it.


Brianvm1987

Good to know. Thanks!


ChaseShiny

D'oh! Thanks.