Numerous sections with tabs on the webpage

I am in need of incorporating multiple tabbed sections on a single page, but unfortunately they are currently interconnected. When one tab is clicked, it impacts the others.

To see this issue in action, you can visit: https://jsfiddle.net/pxpsvoc6/

This is the HTML code snippet:

<div class="recom-content">

    <ul class="tabs">
        <li class="active" rel="overview1">Overview</li>
        <li rel="prices1">Prices</li>
    </ul>
    <!--Close Tabs Menu-->

    <div class="tab_container">

        <h3 class="tab_drawer_heading" rel="overview1">Overview</h3>
        <div id="overview1" class="tab_content">
            <h2>Lamborghini</h2>
            <h1>huracan</h1>
            <hr>
            <p>Adhuidenter us, consuleretis habunte, Caturideo estiaeq uamdien duconsimum cre ni inc re crurarentiam det nonsus; C. Cen Etrei cae perdiere que ta, coneque ina, Cat, popoenatum actatin sultodi factod norum am iusa dica aturnih iciemnenam aperis. Cion</p>
            <br>
            <a href="#"><div class="buttona">Book Now</div></a>
        </div>

        <h3 class="tab_drawer_heading" rel="prices1">Prices</h3>
        <div id="prices1" class="tab_content">
            conetasd
        </div>

    </div>
    <!--Close Tabs Container-->

    </div>
    <!--Close Recom Content-->



        <div class="recom-content">

    <ul class="tabs">
        <li class="active" rel="overview2">Overview</li>
        <li rel="prices2">Prices</li>
    </ul>
    <!--Close Tabs Menu-->

    <div class="tab_container">

        <h3 class="tab_drawer_heading" rel="overview2">Overview</h3>
        <div id="overview2" class="tab_content">
            <h2>Lamborghini</h2>
            <h1>huracan</h1>
            <hr>
            <p>Adhuidenter us, consuleretis habunte, Caturideo estiaeq uamdien duconsimum cre ni inc re crurarentiam det nonsus; C. Cen Etrei cae perdiere que ta, coneque ina, Cat, popoenatum actatin sultodi factod norum am iusa dica aturnih iciemnenam aperis. Cion</p>
            <br>
            <a href="#"><div class="buttona">Book Now</div></a>
        </div>

        <h3 class="tab_drawer_heading" rel="prices2">Prices</h3>
        <div id="prices2" class="tab_content">
            conetasd
        </div>

    </div>
    <!--Close Tabs Container-->

    </div>
    <!--Close Recom Content-->

Below is my CSS styling:

ul.tabs {
    margin: 0;
    padding: 0;
    float: left;
    list-style: none;
    height: 50px;
    width: 100%;
    margin-bottom:10%;
}

ul.tabs li {
    float: left;
    margin: 0;
    cursor: pointer;
    height: 75px;
    width:50%;
    background-color: #000000;
    overflow: hidden;
    position: relative;

    font-family: 'montserratlight';
    font-size:15px;
    line-height:75px;
    color:#ffffff;
    text-transform:uppercase;
    text-align:center;
    margin-bottom:10px;
    letter-spacing:1px;

}

ul.tabs li:hover {
    background-color:#525252;
    color: #ffffff;
}

ul.tabs li.active {
    background-color:#ffffff;
    color: #000000;
    display: block;
    font-family: 'montserratregular';
}

.tab_container {
    border-top: none;
    clear: both;
    float: left;
    width: 400px;
    background-color:#ffffff;
    overflow: auto;
}

.tab_content {
    padding: 10%;
    display: none;
}

.tab_content h2 {
    text-transform:uppercase;
    letter-spacing:3px;
    margin-bottom:4px;
}

.tab_content hr {
    width:20%;
    margin-left:0px;
    margin-top:20px;
    margin-bottom:20px;

    border:1px solid #dadcdf;
}

.tab_content p:last-of-type {
    margin-bottom:0px;
}


.recom-content  {
    width:200px;
    height:auto;
    display:table-cell;
    vertical-align:top;
    text-align:left;
    border:1px solid #dadcdf;
}


.tab_drawer_heading { display: none; }

@media screen and (max-width: 768px) {
    .tabs {
        display: none;
    }
    .tab_drawer_heading {

        font-family: 'montserratregular';
        font-size:13px;
        line-height:50px;
        color:#ffffff;
        text-transform:uppercase;
        margin-bottom:10px;
        letter-spacing:0px;
        text-align:center;

        background-color:#b1b8be;
        margin: 0;
        padding: 2px 0px;
        display: block;
        cursor: pointer;
        border-bottom:1px solid #a8afb7;
        -webkit-touch-callout: none;
        -webkit-user-select: none;
        -khtml-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
    }
    .d_active {
        background-color:#6f787e;
        color: #fff;
    }
}

And here is the JavaScript section:

// Tabbed content functionality
$(".tab_content").hide();
$(".tab_content:first").show();

/* For tab mode */
$("ul.tabs li").click(function() {

  $(".tab_content").hide();
  var activeTab = $(this).attr("rel"); 
  $("#"+activeTab).fadeIn();        

  $("ul.tabs li").removeClass("active");
  $(this).addClass("active");

  $(".tab_drawer_heading").removeClass("d_active");
  $(".tab_drawer_heading[rel^='"+activeTab+"']").addClass("d_active");

});
/* For drawer mode */
$(".tab_drawer_heading").click(function() {

  $(".tab_content").hide();
  var d_activeTab = $(this).attr("rel"); 
  $("#"+d_activeTab).fadeIn();

  $(".tab_drawer_heading").removeClass("d_active");
  $(this).addClass("d_active");

  $("ul.tabs li").removeClass("active");
  $("ul.tabs li[rel^='"+d_activeTab+"']").addClass("active");
});


/* Adding extra class "tab_last" to apply right side border to the last tab */
$('ul.tabs li').last().addClass("tab_last");

Answer №1

The issue at hand is the repeated occurrence of the same code. Using classes alone won't suffice, as each instance shares the same classes.

To address this, you need to specify a parent element that will serve as the basis for applying modifications.

var parent = $(this).parents(".recom-content");
parent.find(".tab_content").hide();

Below is the updated snippet of your code:

// tabbed content
// http://www.entheosweb.com/tutorials/css/tabs.asp
$(".recom-content").find(".tab_content").hide();
$(".recom-content").find(".tab_content:first").show();

/* if in tab mode */
$("ul.tabs li").click(function() {
    var parent = $(this).parents(".recom-content"),
        activeTab = $(this).attr("rel");
    
    parent.find(".tab_content").hide();
    $("#"+activeTab).fadeIn();
    
    parent.find("ul.tabs li").removeClass("active");
    $(this).addClass("active");
    
    parent.find(".tab_drawer_heading").removeClass("d_active");
    parent.find(".tab_drawer_heading[rel^='"+activeTab+"']").addClass("d_active");
    
});
/* if in drawer mode */
$(".tab_drawer_heading").click(function() {
    var parent = $(this).parents(".recom-content"),
        d_activeTab = $(this).attr("rel"); 
   
    parent.find(".tab_content").hide();
    $("#"+d_activeTab).fadeIn();
    
    parent.find(".tab_drawer_heading").removeClass("d_active");
    parent.find(this).addClass("d_active");
    
    parent.find("ul.tabs li").removeClass("active");
    parent.find("ul.tabs li[rel^='"+d_activeTab+"']").addClass("active");
});

/* Extra class "tab_last" 
to add border to right side
of last tab */
$('.recom-content').find('ul.tabs li:last').addClass("tab_last");
ul.tabs {
margin: 0;
padding: 0;
float: left;
list-style: none;
height: 50px;
width: 100%;
margin-bottom:10%;
}

ul.tabs li {
float: left;
margin: 0;
cursor: pointer;
height: 75px;
width:50%;
background-color: #000000;
overflow: hidden;
position: relative;

font-family: 'montserratlight';
font-size:15px;
line-height:75px;
color:#ffffff;
text-transform:uppercase;
text-align:center;
margin-bottom:10px;
letter-spacing:1px;

}

ul.tabs li:hover {
background-color:#525252;
color: #ffffff;
}

ul.tabs li.active {
background-color:#ffffff;
color: #000000;
display: block;
font-family: 'montserratregular';
}

.tab_container {
border-top: none;
clear: both;
float: left;
width: 400px;
background-color:#ffffff;
overflow: auto;
}

.tab_content {
padding: 10%;
display: none;
}

.tab_content h2 {
text-transform:uppercase;
letter-spacing:3px;
margin-bottom:4px;
}

.tab_content hr {
width:20%;
margin-left:0px;
margin-top:20px;
margin-bottom:20px;

border:1px solid #dadcdf;
}

.tab_content p:last-of-type {
margin-bottom:0px;
}


.recom-content{
width:200px;
height:auto;
display:table-cell;
vertical-align:top;
text-align:left;
border:1px solid #dadcdf;
}


.tab_drawer_heading { display: none; }

@media screen and (max-width: 768px) {
.tabs {
display: none;
}
.tab_drawer_heading {

font-family: 'montserratregular';
font-size:13px;
line-height:50px;
color:#ffffff;
text-transform:uppercase;
margin-bottom:10px;
letter-spacing:0px;
text-align:center;

background-color:#b1b8be;
margin: 0;
padding: 2px 0px;
display: block;
cursor: pointer;
border-bottom:1px solid #a8afb7;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.d_active {
background-color:#6f787e;
color: #fff;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div class="recom-content">
    <ul class="tabs">
        <li class="active" rel="overview1">Overview 1</li>
        <li rel="prices1">Prices 1</li>
    </ul>
    <!--Close Tabs Menu-->
    
    <div class="tab_container">
        
        <h3 class="tab_drawer_heading" rel="overview1">Overview</h3>
        <div id="overview1" class="tab_content">
            <h2>Lamborghini</h2>
            <h1>huracan</h1>
            <hr>
            <p>Adhuidenter us, consuleretis habunte, Caturideo estiaeq uamdien duconsimum cre ni inc re crurarentiam det nonsus; C. Cen Etrei cae perdiere que ta, coneque ina, Cat, popoenatum actatin sultodi factod norum am iusa dica aturnih iciemnenam aperis. Cion</p>
            <br>
            <a href="#"><div class="buttona">Book Now</div></a>
        </div>
        
        <h3 class="tab_drawer_heading" rel="prices1">Prices</h3>
        <div id="prices1" class="tab_content">
            conetasd
        </div>
        
    </div>
    <!--Close Tabs Container-->
    
</div>
<!--Close Recom Content-->



<div class="recom-content">
    
    <ul class="tabs">
        <li class="active" rel="overview2">Overview 2</li>
        <li rel="prices2">Prices 2</li>
    </ul>
    <!--Close Tabs Menu-->
    
    <div class="tab_container">
        
        <h3 class="tab_drawer_heading" rel="overview2">Overview</h3>
        <div id="overview2" class="tab_content">
            <h2>Lamborghini</h2>
            <h1>huracan</h1>
            <hr>
            <p>Adhuidenter us, consuleretis habunte, Caturideo estiaeq uamdien duconsimum cre ni inc re crurarentiam det nonsus; C. Cen Etrei cae perdiere que ta, coneque ina, Cat, popoenatum actatin sultodi factod norum am iusa dica aturnih iciemnenam aperis. Cion</p>
            <br>
            <a href="#"><div class="buttona">Book Now</div></a>
        </div>
        
        <h3 class="tab_drawer_heading" rel="prices2">Prices</h3>
        <div id="prices2" class="tab_content">
            conetasd
        </div>
        
    </div>
    <!--Close Tabs Container-->
    
</div>
<!--Close Recom Content-->

Answer №2

To properly hide the specific elements, you should change the statement as follows:

$(".tab_content").hide(); 

Change it to:

$(this).parents(".recom-content").find(".tab_content").hide();

The original selector is hiding all tab_content items instead of just the ones that are descendants of its parent recom-content DIV.

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

Merging text and a JSON object to retrieve the information

Having some trouble with a JSON object and retrieving values. This is the syntax that works for getting the data I need. dataJSON.companies[0].fields.Internet.length I want to dynamically evaluate the object using a string variable, like this... var me ...

Tips for Sharing Multiple Nested Arrays in AngularJS

I am working with an array in AngularJS, and here is an example: $scope.order.qty='20'; $scope.order.adress='Bekasi'; $scope.order.city='Bekasi'; To post this array, I use the following code: $http({ method : &ap ...

The function $scope.removeNinja cannot be found

As a beginner in Angular, I encountered an issue while working on a project that involved creating a Directory of ninjas with various functionalities implemented using Angular. Here is a snippet of my HTML file: <!DOCTYPE html> <html lang="en" n ...

Backdrop styling for Material-UI dialogs/modals

Is there a way to customize the semi-transparent overlay of a material-ui dialog or modal? I am currently using material-ui with React and Typescript. https://i.stack.imgur.com/ODQvN.png Instead of a dark transparent overlay, I would like it to be transp ...

The initial item on the list, displayed on the right side in Google Chrome

Currently experiencing an issue with Google Chrome where the first list item's bullet is floating right while all other list items' bullets are aligned correctly at left. Here is the code snippet causing the problem: <div class="window_sub_ ...

When using Codeigniter 3.1.11 and Jquery (AJAX), there seems to be an issue with retrieving the value of csrf_cookie_name using $.cookie

After going through multiple discussions on stackoverflow and various articles online, I have attempted all suggested solutions for CSRF issues when sending an AJAX request via JQuery, but so far, nothing has worked for me. Codeigniter Version: 3.1.11 JQu ...

Hiding content in HTML with the Display:none property

After exploring various posts on this topic, I am still unable to find a solution that fits my specific scenario. Despite the challenges, I thought it would be worth asking for recommendations. Currently, I have a PowerShell script generating a report in ...

Counting the elements on a page using Selenium and Node.js: A step-by-step guide

I've been experimenting with Selenium in Javascript using NodeJS and I'm trying to tally up some elements based on CSS selectors. So far, I've attempted a few methods: client.findElements(By.css(".some-class")).size(); However, I encounte ...

A basic webpage created using ASP.NET featuring simple HTML text

Is it acceptable to manually code HTML, such as <p>, <ul>/<li>, <h3>, <h4>, <blockquote> tags, into a Content Page that already has an existing Master Page? The content is similar to a blog post. Are there better desig ...

The box on my form is leaking my background color outside the 1px border, but only in Internet Explorer

While experimenting with background gradients and borders one day just for the fun of it, I made an interesting observation in Internet Explorer. The issue I encountered was that the background color was overflowing outside the radius border, but this onl ...

Incorporate Aria Label into a Website Link

Currently working on enhancing website accessibility. I have identified a close menu button that lacks an Aria Label, and my goal is to rectify this using JavaScript. Although I am utilizing the script below to target the specific ID and add the attribute ...

What is the best way to assign an active class to a specific footer ID on an HTML page using AngularJS?

I tried using the activeLink directive to apply an active class to a specific id on the page, but it didn't work as expected. .directive('activeLink', ['$location', function (location) { return { restrict: 'A', ...

What is the best method for incorporating new data into either the root or a component of Vue 3 when a button is pressed?

One issue I'm facing is the challenge of reactively adding data to either the Vue root or a Vue component. After mounting my Vue app instance using app.mount(), I find it difficult to dynamically add data to the application. As someone new to the fram ...

Inaccurate AJAX response mentioned

For populating text boxes from a database, I am using an onChange event. Although the code runs, the response I get is incorrect. The code I am using can be found below: index.php <?php $servername = "localhost"; $username = "root"; $password = ""; $ ...

You can obtain the values of multiple div selections using jQuery

In a display with two columns, I want to be able to perform a common operation (such as delete) based on the selection of certain div IDs. If a user selects the div IDs participant_1, participant_4, participant_6 at the same time, I need to collect these ...

Toggle the sidebars to slide out and expand the content division using JavaScript

I am looking to achieve a smooth sliding out effect for my sidebars while expanding the width of the middle content div. I want this action to be toggled back to its original state with another click. Here's what I've attempted so far: $(' ...

CSS for Positioning Elements Side by Side Using Absolute Positioning

I'm having trouble figuring out how to position balloons like the ones shown in this image: <div class="red_frame"> <img src="https://i.sstatic.net/54SMF.png" class="r_over"/> <img src="https://i.sstatic.net/54SMF.png" ...

Oops! An error has occurred: The requested method 'val' cannot be called on an undefined object

I am struggling with this issue. This is the code that I am currently working on: http://jsfiddle.net/arunpjohny/Jfdbz/ $(function () { var lastQuery = null, lastResult = null, // new! autocomplete, processLocation = function ...

Receiving the error "Potential null object. TS2531" while working with a form input field

I am currently working on developing a straightforward form to collect email and password details from new users signing up on Firebase. I am utilizing React with Typescript, and encountering an error labeled "Object is possibly 'null'. TS2531" s ...

Using Font Color in Gmail HTML Emails

While working on styling an HTML email, I've encountered an unexpected issue with inline CSS in Gmail. I set the color of text in a table row using the following code: <tr style='color: #000; font-size: 14px; font-family: Verdana, serif;' ...