I recently implemented a tab system with panels that seems to be working well when switching between tabs. However, I'm facing an issue where clicking on the same tab does not hide the corresponding panel and reset the aria attributes as intended. I've attempted a solution, but it has not yielded the desired results.
This is the approach I took:
if (thisTab.attr('aria-expanded') == 'true') {
thisTab.attr('aria-expanded', 'false');
thisTab.removeClass('is-active');
thisTabPanel.attr('aria-hidden', 'true');
} else {
thisTab.attr('aria-expanded', 'true');
thisTab.addClass('is-active');
thisTabPanel.attr('aria-hidden', 'false');
}
Thank you for any assistance you can provide!
$(document).ready(function() {
$('.tablist li').attr('role', 'presentation');
$('.tablist li a').attr({
"aria-selected": "false",
"role": "tab",
"tabindex": "0"
});
$('.tablist__panel').attr({
"aria-hidden": "true",
"role": "tabpanel"
});
$('.tab').attr('id', function(IDcount) {
return 'tab' + IDcount;
});
$('.tab').attr('href', function(IDcount) {
return '#panel' + IDcount;
});
$('.tab').attr('aria-controls', function(IDcount) {
return 'panel' + IDcount;
});
$('.panel').attr('id', function(IDcount) {
return 'panel' + IDcount;
});
$('.panel').attr('aria-labelledby', function(IDcount) {
return 'tab' + IDcount;
});
$(".panel").hide();
$(function() {
// Cache selectors
var tabs = $('.tablist li a'),
tabPanels = $('.panel');
tabs.on('click', function() {
event.preventDefault();
var thisTab = $(this),
thisTabPanelId = thisTab.attr('aria-controls'),
thisTabPanel = $('#' + thisTabPanelId);
tabs.attr('aria-selected', 'false').removeClass('is-active');
thisTab.attr('aria-selected', 'true').addClass('is-active');
tabPanels.attr('aria-hidden', 'true').hide();
thisTabPanel.attr('aria-hidden', 'false').show();
//HERE'S WHAT I CAN'T GET TO WORK
if (thisTab.attr('aria-expanded') == 'true') {
thisTab.attr('aria-expanded', 'false');
thisTab.removeClass('is-active');
thisTabPanel.attr('aria-hidden', 'true');
} else {
thisTab.attr('aria-expanded', 'true');
thisTab.addClass('is-active');
thisTabPanel.attr('aria-hidden', 'false');
}
});
});
});
.wrapper {
max-width: 500px;
margin: 0 auto;
}
.tablist {
display: flex;
margin: 0;
padding: 0;
list-style: none;
}
.tab {
display: inline-block;
margin: 15px;
padding: 15px;
background-color: gray;
color: white;
cursor: pointer;
}
.tab.is-active {
font-weight: 700;
background-color: gray;
}
.panel {
border: 1px solid gray;
margin: 15px;
padding: 0 15px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
<ul class="tablist" role="tablist">
<li><a class="tab">Tab 1</a></li>
<li><a class="tab">Tab 2</a></li>
<li><a class="tab">Tab 3</a></li>
</ul>
<div class="panel">
<p>PANEL 1 Pellentesque in ipsum id orci porta dapibus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus suscipit tortor eget felis porttitor volutpat.</p>
</div>
<div class="panel">
<p>PANEL 2 Pellentesque in ipsum id orci porta dapibus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus suscipit tortor eget felis porttitor volutpat.</p>
</div>
<div class="panel">
<p>PANEL 3 Pellentesque in ipsum id orci porta dapibus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus suscipit tortor eget felis porttitor volutpat.</p>
</div>
</div>