Skip to content
Advertisement

jQuery tabs: how to addClass to a separate UL tab from the tab div?

I am trying to set up a tabs nav with jQuery without using the jQuery Tabs UI. From what I understand, when the user clicks the list element, the code

  • grabs the list element with data-tab="X" and adds the class current which will set the opacity: 1 for that link (default: 50%),
  • then removes the current class from the tabs content section and displays the corresponding div with the same data-tab attribute.

I tried to replicate the same process with a button click. This seems to work as it removes the current class from the .tab-link classes, but doesn’t add the current class to the corresponding ul.tabs li element. This creates a confusing UX as it’s not clear what section/tab is active/current when the user presses the next/previous buttons.

How can I add the current class to the corresponding ul.tabs li when I click the next and previous buttons? Is this a jQuery issue or a CSS issue?

JavaScript
JavaScript
JavaScript

Advertisement

Answer

You just need to select the li with the data-tab with your tab_id, e.g.

JavaScript

This uses the tab_id variable you create and looks for an element with the data-tab value set to this, e.g. data-tab="tab-2"

You can see it working here:

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