Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML/CSS show/hide multiple elements?

Tags:

html

css

I'm looking for an HTML/CSS solution to this challenge: I have multiple elements with the same class or same id, and I want to show/hide them all at the same time, using a button or toggle switch. So I then have a click event, when I click that class or ID representing all those elements, they all hide. When I click again, they must show again.

I would appreciate

Thanks

like image 870
DextrousDave Avatar asked Mar 10 '26 13:03

DextrousDave


1 Answers

HTML and CSS are used to describe a static representation of content - there is no way dynamically hide/show content using HTML/CSS. You would need to use Javascript to do this. Code example (very simplistic and unelegant example):

<div id="somediv">Hide This</div>
<input type="button" onclick="hide('somediv')" value="Hide Div"/>
<script type="text/javascript">
     function hide(div_id) {
           document.getElementById(div_id).style.display = "none";
     }
</script>

A nicer solution would be to use jQuery but I think for your case you should first learn the basics of Javascript and HTML/CSS before moving onto jQuery.

like image 56
christophmccann Avatar answered Mar 12 '26 02:03

christophmccann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!