Setting Active Tab in Bricks’ Tabs (Nestable) Element

Setting Active Tab in Bricks’ Tabs (Nestable) Element

In the Bricks Facebook group a user asks:

I have a Nestable tab with 2 tabs. how can I determine which tab is the default active?

We can do this with CSS alone or custom JavaScript (thanks, ChatGPT).

By default, the first tab item will be the active one.

CSS

Move the Title and Pane elements of the tab you want to be initially focused/active to be at the top of the structure.

Add this custom CSS in the Tabs (Nestable) element:

%root% > .tab-menu .tab-title:nth-child(2) {
	order: -1;
}

Before:

After:

JavaScript

Adding the following inside the Bricks editor at Settings (gear icon) → PAGE SETTINGS → CUSTOM CODE → Body (footer) scripts will make the 2nd tab of a Tabs (Nestable) element active instead on page load:

<script>

// Simulate a mouse click on the second tab to activate it on page load
document.addEventListener('DOMContentLoaded', function() {
  // Get the second tab element
  const secondTab = document.querySelector('#brxe-xmqjft .tab-title:nth-child(2)');

  // Function to simulate a mouse click event
  function simulateClick(element) {
    const event = new MouseEvent('click', {
      bubbles: true,
      cancelable: true,
      view: window
    });
    element.dispatchEvent(event);
  }

  // Simulate a mouse click on the second tab
  simulateClick(secondTab);
});

</script>

Repalce #brxe-xmqjft with the HTML ID of the “Tab menu” element.

Before:

After:

Reference

https://x.com/srikat/status/1732725019749523705

Get access to all 524 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
How to Link to a Specific Tab in Bricks

How to Link to a Specific Tab in Bricks

This Pro tutorial provides the JavaScript code that will automatically switch to the tab when using Nestable Tabs elements based on the "tab" URL parameter…
Categories:
How to sync two nestable sliders in Bricks

How to sync two nestable sliders in Bricks

In this tutorial, we'll learn how to sync two nestable sliders in Bricks: one will be the main slider and the other one will be…
Categories:
Opening RSS Block’s Links in New Tabs

Opening RSS Block’s Links in New Tabs

Looking to have links output by RSS Block (in the block editor) in new tab/window when using Bricks? Add this in your Bricks template/Page at…
Categories:
How to Create a Grid Slider in Bricks

How to Create a Grid Slider in Bricks

In this tutorial, we'll learn how to create multiple rows and columns inside the default nested slider of Bricks. Introduction A user recently asked in…
Categories:
Pro
[WooCommerce] Sticky on Scroll Add to Cart section in Bricks

[WooCommerce] Sticky on Scroll Add to Cart section in Bricks

Setting up a sticky section that fades in when scrolling down and fades away when scrolled to the top.
Categories:
Pro
WP Grid Builder Filters inside Bricks’ OffCanvas

WP Grid Builder Filters inside Bricks’ OffCanvas

This Pro tutorial provides the steps to move WP Grid Builder's filters inside the Bricks' native Offcanvas on smaller viewport widths. Let's work with the…
Enqueueing a JavaScript File in Bricks

Enqueueing a JavaScript File in Bricks

Bricks child theme's functions.php comes with code to enqueue (load) style.css on the front end. What if you want to load a custom JavaScript file…
Categories: