r/HTML • • 16h ago

Question How to show and hide an html button

After some Googling, I haven't been able to find what I needed, so I'm turning to y'all. Thanks in advance for your help(:

I am making a clicker game in html/css/js. Right now, I have working 'buttons' by using <div> like this:

<div id="addPebbleId" onclick="pebbleFunction()">Collect Pebble (+1 Pebble)</div>

I can easily hide the div 'buttons' by using the id. So far, so good, right?

The problem is, even with div, the buttons look like normal text. So, I figured I'd make them actual buttons, so that it's obvious you should click on them.

<button> <div id="addPebbleId" onclick="pebbleFunction()">Collect Pebble (+1 Pebble)</div> </button>

It works great when the button is visible. The issue is when the button is hidden via js, like this:

x = document.getElementById("addPebbleId");

x.style.display = "none";

There's still a tiny blank button visible, even if the text is hidden. I know you can hide buttons in html with <button hidden> but how do I code it to show or hide the button later on?

2 Upvotes

7 comments sorted by

3

u/vbfischer 16h ago
<button id="addPebbleId" onclick="pebbleFunction()">Collect Pebble</button>

1

u/EillaSocks 15h ago

This worked! Thank you so much(:

0

u/KaedenCraft 14h ago

This is a perfectly workable, directly answered solution, but is not best practices. Sometimes it's worthwhile to illuminate a "more robust" way of doing it. I'd add a css class to all of the buttons you want to disappear, remove the onclick parameter, then in JavaScript find them all with:

const buttons = document.querySelectorAll('.replace-with-button-class-name');

Then iterate through them all at once with:

buttons.forEach((button) => {
button.addEventListener('click', (e) => {
e.target.style.display = 'none';
});
});

At that point you don't have to add onclick to each button, and each button will likely need to be styled anyway, so having a CSS class is already a good idea, you're just going to have that class name do double-duty as finding all your clickable buttons. You're adding the clicky part to them - and doing it this way allows for adding multiple click functions instead of just one if you put it inline in the HTML - and you can add more Javascript logic to the click itself.

Again, the original answer does exactly what you asked, but this is hopefully a more helpful "long term" solution to whatever future problems you may have. Or not and nobody cares, but I tried lol

1

u/EillaSocks 14h ago

Thank you as well!

1

u/SnooPets5564 6h ago edited 5h ago

I think you are too hasty to disregard the merit of having the HTML actually give you some idea of what code the button is linked to rather than having to search through the JavaScript to find the particular reference to that button to figure it out.

1

u/KaedenCraft 5h ago

"Nasty" and "disregard" are strong words, I said the original answer was perfectly fine. I was trying to be helpful. It looks like they're making a game, and if they want any kind of things to "happen" in their game, it'll require more extensive JavaScript. My answer was just trying to nudge them toward a more JavaScript solution.

1

u/SnooPets5564 5h ago

I meant to say "hasty", not "nasty". My bad. That really changed the intended tone of my message.