7th Dec '23
/
0 comments

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 610 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

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:
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:
How to disable smooth scroll in Bricks

How to disable smooth scroll in Bricks

In certain situations like using ScrollSmoother, you may be looking to disable smooth scrolling functionality that’s built into Bricks.
Categories:
McDonald’s Order Promise Analogy

McDonald’s Order Promise Analogy

From the JavaScript course I am doing now: A Promise is like placing an order at a restaurant. You receive a ticket (Promise object) that…
Categories:
Pro
Button-specific HTML in Bricks Popup

Button-specific HTML in Bricks Popup

In the Bricks Facebook group a user asks: I need to create popups to expend the text (eg. read more). I wonder if I have…
Categories:
Pro
Opening Submenus with Click Instead of Hover in Bricks

Opening Submenus with Click Instead of Hover in Bricks

This Pro tutorial provides the code for opening submenus with click instead of hover in Bricks. Step 1 Add this in child theme's style.css: Step…
Categories:
How to change the text of an element based on breakpoints without creating duplicated content

How to change the text of an element based on breakpoints without creating duplicated content

In this tutorial, we'll learn how to dynamically change the text of a basic text element based on the viewport width in Bricks. Introduction Sometimes…
Categories: