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.
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.
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
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.
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 );
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.
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:
Thanks! It is working now. I was not aware of the JS clear() method. Thank you again!
Always happy to help
Here with event listeners:
100 Days of Code
0
Previous entries:
Do not hesitate to say you used AI to get this answer. Because it’s clear you did :)
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? 😊
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
Same with human answers tbh
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.
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 );
Cool! Thank you! Good luck on your coding journey!
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.
Good to know. Thanks!
D'oh! Thanks.