Skip to content
Advertisement

How to toggle two elements correctly with two buttons?

I want to show cards when cards is clicked and show table when table is clicked. But by default, one of them needs to show at the beginning with the other hidden. But if either button is clicked on again the div will stay to what is clicked on. I have it half way working. If you start on the table button first you will see it working until you click again (same button) or start on the cards button. Any help is appreciated. Here is an example of what I need.

JavaScript
JavaScript

Advertisement

Answer

Here’s one way to approach it. Use classes to consolidate your functions, then use a data-attribute to designate which button reveals what item

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement