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

Leave the first comment

 

Related Tutorials..

Pro
Grid.js in Bricks

Grid.js in Bricks

This Pro tutorial provides the steps to use Grid.js in a WordPress site powered by Bricks builder. Grid.js is a lightweight and performant JavaScript library…
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:
Pro
tsParticles in Bricks

tsParticles in Bricks

Updated on 5 Aug 2023 This Pro tutorial provides the steps to set up tsParticles, a lightweight TypeScript (uses JavaScript) library for creating particles as…
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:
Pro
Dynamic Tabs in Bricks

Dynamic Tabs in Bricks

This Pro tutorial provides the steps to show the terms of a taxonomy as tab titles and corresponding posts as tab panes using a Tabs…
Categories:
Tags:
Pro
Post Title at a Different Location on Image Hover

Post Title at a Different Location on Image Hover

This Pro tutorial provides the steps to update the text of a sticky div with the title of the post that is hovered on the…
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: