Conceal tab content by clicking outside of the tab or its contents

Here is an example of how Elementor tab works. When you click on a tab, the content section appears. I want to be able to close the content section by clicking on any other part of the page, except for the tabs and tab content themselves. This should not affect the functionality of the tabs - if I click on a tab again, it should show its content as usual.

When a tab is clicked, a class "tab-current" is added to change the background color of the tab. The active content is displayed with a class "content-current".

jQuery(document).ready(function() {
  jQuery(document).click(function(event) {
    if (!jQuery(event.target).closest('.premium-tabs-nav-list-item').length) {
      jQuery('.premium-tabs-nav-list-item').removeClass('tab-current');
    }
    if (!jQuery(event.target).closest('.premium-tabs-content-section').length) {
      jQuery('.premium-tabs-content-section').removeClass('content-current');
    }

  });
});
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<div class="tabs">
  <div class="premium-tabs-nav-list-item">Tab 1</div>
  <div class="premium-tabs-nav-list-item">Tab 2</div>
  <div class="premium-tabs-nav-list-item">Tab 3</div>
</div>
<div class="content">
  <div class="premium-tabs-content-section">Content 1</div>
  <div class="premium-tabs-content-section">Content 2</div>
  <div class="premium-tabs-content-section">Content 3</div>
</div>

Answer №1

Adding some CSS to provide a complete preview would be awesome. As of now, I have come up with a suggestion. I've included a simple CSS to demonstrate it.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Elementor Tab Example</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>

<div class="tabs">
  <div class="premium-tabs-nav-list-item">Tab 1</div>
  <div class="premium-tabs-nav-list-item">Tab 2</div>
  <div class="premium-tabs-nav-list-item">Tab 3</div>
</div>
<div class="content">
  <div class="premium-tabs-content-section">Content 1</div>
  <div class="premium-tabs-content-section">Content 2</div>
  <div class="premium-tabs-content-section">Content 3</div>
</div>

</body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Verifying the invocation of a callback function through the use of $rootScope.$broadcast and $scope.$on

I have been testing to see if a callback was called in my controller. Controller (function () { 'use strict'; angular .module('GeoDashboard') .controller('CiudadCtrl', CiudadCtrl); CiudadCtrl.$i ...

Is it possible to incorporate RequireJS in importing VueJS components?

I am currently working on a project that involves using both RequireJS and Vue components. One of my goals is to break down the Vue components into smaller pieces for better organization, and I want to be able to export these smaller components so they can ...

Why does the "error loading page" message appear on the server when there is a PHP function inside jQuery Mobile?

I am facing an issue with a function I created in my jQuery Mobile code. It works perfectly fine on my local machine, but when I host it on the server, it does not work properly. Can someone please assist me? Here is a glimpse of the code: <!DOCTYPE ht ...

Storing various entries in a database by utilizing Laravel/JQuery's dynamic input fields

I am facing an issue while trying to insert data into a database from dynamic input fields. Each additional input field should be inserted into the database as a new row with a primary key. Below is the view and script that I am using to add dynamically g ...

Utilize Java to launch the HTML file in a web browser

I have a specialized need for my HTML files to be displayed on a platform that is browser-free. The solution that immediately comes to mind for me in this scenario is utilizing applet. Therefore, I am interested in having my HTML file (including CSS and JS ...

Bootstrap grid columns cannot properly handle overflowing content

Utilizing bootstrap version 5.2.3 I'm facing a challenge not with bootstrap itself, but rather my limited comprehension of the flex system and overflow. Displayed below is a scenario where the first div has a fixed height of 100vh. My goal is to kee ...

Generate options for a dropdown menu based on the choice made in another dropdown menu utilizing ajax technology

Greetings, I am currently working on a JSP page that features a form with two drop-down lists. The first drop-down is designed to display the country names. While the second drop-down should show the states corresponding to the selected country. I have ...

Exploring threejs to project onto a cylindrical surface

Can you provide guidance on how to project an image onto a cylinder using threejs? Do I need to map separate portions of the image onto each face of the cylinder, or is there a more efficient method available? Appreciate any help on this matter! ...

In React, components will consistently render the initial code

I have a chat application that requires authentication and uses cookies. Here's what I've been attempting: class AppHeader extends React.Component { constructor(props) { super(props) } render() { if (cookies.get(' ...

How can I trigger my function in jQuery after inputting into the jQuery text field?

Looking for advice on implementing a function in jQuery for a text field that is not working as expected. The code works fine when creating a text field in HTML, but encounters issues with the jQuery text field. <!DOCTYPE html> <html> <body ...

Is there a way to convert inline styling to CSS using Material-ui within a React component?

In my development work with React, I initially utilized inline styling to customize the appearance of material-UI components. However, I have come to realize that utilizing CSS provides a more structured and efficient approach to styling components. Despit ...

Send only the modified form fields when making an AJAX PUT request to the REST API

I need to update my data using an AJAX request triggered by clicking the Save button on a form with multiple fields. Currently, the update only works when all fields are filled out because I'm passing $('#field').val(). But what if I only wa ...

Using Mocha with the --watch flag enabled causes issues with ES6 modules and results in error messages

I've been attempting to configure Mocha to automatically monitor for changes in my files using the --watch flag. I have defined two scripts in package.json as follows: "test": "mocha", "test:watch": "mocha --watch ./test ./game_logic" When I run ...

When attempting to delete a resource using an HTTP request in Insomnia, I encountered a TypeError stating that it was unable to read the property 'id'

I recently started using Express and am in the process of setting up the Delete function for my database within the MERN stack. While testing my CRUD operations using Insomnia, I have encountered an issue specifically with the Delete operation. The proble ...

Creating several Doughnut Charts within a single Canvas using Chart.js

Is there a way to display multiple layers of a doughnut chart simultaneously using Chart.js? Currently, I'm only able to see one layer at a time and the others become visible upon hovering over their position... If you have any insights on how to mak ...

Is there a way to activate ng-change when a space character is input?

My function is triggered when the value of a textarea changes, however, it does not work when the spacebar is pressed. Is there a way to make it work for spacebars as well? I want the function to be called whenever the content changes, regardless of the ke ...

Angular throws an error when trying to parse undefined data outside of an async function

I'm having trouble parsing data retrieved from an http call and passing it to ngOnInit. Can you assist me in finding a solution? My project is built with Angular 4. Here's the async function: async getAsyncData() { this.asyncResult = awai ...

Having trouble with your UI Router states not updating correctly when changing the URL in your Angular UI Router?

I have tried numerous solutions and tutorials already with no success in finding the right answer. Therefore, I am posting here in hopes of receiving a fresh perspective and a little challenge. Using: angular ui router The Issue I have set up differen ...

retrieving information from rechart's hover/tooltip feature

I'm currently attempting to access the data object created by recharts that is used to populate tooltips. My goal is to display this data elsewhere. The screenshot below displays the result of console.log(data.payload[0]); This data is accessed thro ...

TypeError: "Table" has not been declared

This is my first experience with the script editor. I have been given the task of creating a pivot table for Google Sheets using a script. // This script creates a pivot table in Google Sheets function createPivotTable() { var ss = SpreadsheetApp.getAc ...