How to addClass and removeClass on onclick hash id url to jquery

WordPrss single page on addClass and removeClass click on button, Here hash tag link id to (#tab-2) on id button click to open new page with hash id url, there custom tabs button to div class show and div class hide to jQuery custom code.

Show and hide classes on onclick hash id url to jquery

Button Click
This one jQuery code for custom tab on onclick addClass and removeClass to jQuery.
<script>
jQuery(document).ready(function(){
jQuery('.cust_tab').on('click', function(){
jQuery('.cust_tab').removeClass('actives');
jQuery(this).addClass('actives');
}); });
</script>
and also below code for hash url(#tab-2) to addClass and removeClass on custom tab buttons and you can tabs with data show and hide to custom jQuery code with we are using switch and case condations for page has tab with links ids.
<script>
window.addEventListener('DOMContentLoaded', function() {
jQuery(document).ready(function () {
var url = window.location.href;

switch (url) {
case 'https://example.com/#tab-2':
jQuery('#tab-2').addClass('actives');
jQuery('#tab-1').removeClass('actives');
jQuery("#data-right").show();
jQuery("#data-left").hide();

break; case 'https://example.com/':
jQuery('#tab-1').addClass('actives');
jQuery('#tab-2').removeClass('actives');
jQuery("#data-left").show();
jQuery("#data-right").hide();
break; }
});
});
</script>

Post a Comment

0 Comments