The following is one approach:
First add a wrapper div over the divs that you click. This will make it easier to add event handlers:
<div id="clicks"> <div id="tab1">Test 1</div> <div id="tab2">Test 2</div> <div id="tab3">Test 3</div> <div id="tab4">Test 4</div> <div id="tab5">Test 5</div> </div>
Then wrap all your divs that you show / hide with another div. This will make it easier to show / hide all divs:
<div id="tabShows"> <div id="tab1show">test 1 default = show</div> <div id="tab2show">test 2</div> <div id="tab3show">test 3</div> <div id="tab4show">test 4</div> <div id="tab5show">test 5</div> </div>
Then use this event handler:
// when user click on any div inside div 'clicks' $('#clicks').on('click', 'div', function(event){ // Get the id of a div that was clicked, eg tab1 var tabId = $(this).attr('id'); // Hide all divs inside div 'tabShows' $('#tabShows div').hide(); // Show only div that was clicked, eg tab1show $('#' + tabId + 'show').show(); });
Here is the link for updated jsFiddle
source share