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

Navigate to a new webpage using a string of characters as a legitimate web address

When a user performs a search, I pass the search term as a string to direct them to a new page. How can I create a valid URL (e.g., remove spaces from the string)? It's crucial that the next page can recognize where any spaces were in the search word ...

Grails project: Attempting implementation of tablesorter

I have a question regarding utilizing table sorter with my Grails project. I have successfully copied the jquery.tablesorter.js file into the 'js' directory, allowing me to make columns sortable on click. However, I am curious about accessing the ...

PHP: When MySQL is not returning results and an undefined variable is causing issues

Currently, I am engaged in a project where I need to fetch some data from the database and display it within a form. Although my SQL query seems correct when reviewed in the SQL log, MySQL is not returning any results. I am seeking assistance on how to ret ...

Link clicking causing webpage navigation issues

My web page features a table of contents, but unfortunately the page navigation is not functioning properly. Below is the HTML code: <a name="top_of_page">Create Account</a> <a href="#top_of_page">top</a> Even when I click on the ...

Navigating on Blogger can be a tricky task when it comes to searching and

I recently added a Table to my blogger post, along with an input form for readers to easily search data. After finding the code on W3Schools (link here), I implemented it successfully. However, I am looking to make some improvements. Here is my question: ...

Storing data on your local machine using Electron

I am in need of help with my template files which have variable strings. I want to create a basic input form using Electron (https://www.electronjs.org/) and save the resulting output file on the user's device. Could someone recommend a module that e ...

How can the first paragraph value of a div be found using jQuery in Next.js?

How can I retrieve the value of the first <p> tag within my div when a button inside the same div is clicked? Despite attempting to use $(this), it doesn't appear to be functioning as expected. Take a look at the code snippet below: <div cla ...

Entering numerous numerical values across a variety of input fields

My website currently has a form with 6 input fields where visitors need to enter a 6 digit code. To make it easier for them, I want to allow users to simply paste the code we provide into the first input field and have the remaining digits automatically po ...

Vue: nesting components within components

Looking for a clever solution to create a nested component system with efficient rendering? Check out the code snippet below: custom-tab.vue (child component) <template> <slot></slot> </template> <script> export def ...

Error: 'concurrently command not recognized' even though it was installed globally

I am currently facing an issue on my macOs system. Even though I have installed concurrently globally through npm, whenever I set it as a start script in my package.json file and try running npm start, I encounter the following error. concurrently - k ...

Leverage the power of Laravel8 to simultaneously update two tables in one go

In my database, I currently have two tables: https://i.stack.imgur.com/TObcy.jpg https://i.stack.imgur.com/U6IhT.jpg The form that I am working with is shown below: <form id="contratform" method="post" action="contrats" ...

Ways to determine the port being utilized in NodeJS

After executing the process with npm run start I want to keep track of the port being used. Is there a command available for this monitoring purpose? ...

Http post request failing to execute

I'm having an issue with an ActionResult that has the HttpPost attribute. It seems like I need this attribute when the user selects a date from a DateTimePicker. In my scenario, I have 2 DateTimepickers of type @html.EditorFor. These pickers correspon ...

The most secure method for transmitting a JSON string to a PHP server

I've been attempting to transmit a JSON string to a PHP server using the following code: $.ajax({ type: "POST", url: "themes.php?page=themeoptions", data: {structure : JSON.stringify(structure)}, }); However, all the quotation ...

Parameterized function call on click event

There is a function defined as follows: menu[0].onclick = function() { filters.reset_all(); clients.get(); } This function gets called when the user clicks on the first menu element. Now, I need to invoke this function from another place and here ...

Ways to guide users through a single-page website using the URL bar

I currently have a one-page website with links like <a href="#block1">link1</a>. When clicked, the browser address bar displays site.com/#block1 I am looking to modify this so that the browser shows site.com/block1, and when the link is clicke ...

Is the `beforeEach` function in Jasmine synchronized?

Is it guaranteed that multiple beforeEach functions will always run sequentially? beforeEach(function() {}); beforeEach(function() {}); beforeEach(function() {}); beforeEach(function() {}); beforeEach(function() {}); It appears they do. I conducted a tes ...

The "hidden" class in Bootstrap 4 is not functioning as expected

I have Bootstrap v4 set up with its default CSS and JS. I'm attempting to use the classes hidden, hidden-XX-down, and hidden-XX-up on different divs, buttons, etc. However, it doesn't seem to be having any effect. All other classes are working fi ...

Discover the hyperlink and submit it via the form to the designated email address

Is it possible to create a basic HTML form that captures name and email details, then uses jQuery to locate a specific link on the page and includes this link along with the form information in a submission to a web server? From there, a script could send ...

Reposition HTML content using regular expressions

My objective is to locate a block of HTML that contains comments and relocate it after the header section. The comment itself remains constant, but the content within the block varies across different pages and needs to be replaced using Dreamweaver' ...