Shifting the position of personalized tabs using Jquery

I have created some basic HTML/CSS tabs and I want to move to the next tab by clicking a link at the bottom of every tab.

HTML

 <ul class="tabs">
   <li class="active">
      <a href="#">Explainer (2mins)</a>
      <div class="content">
         <div id="Video" class="tabcontent">
            <div class="coltab">
               content A
            </div>
            <h4>Next tab: View Some Sample Lessons</h4>
         </div>
      </div>
      </div>
   </li>
   <li>
      <a href="#">Sample Lessons</a>
      <div class="content coltab">
         <div>
            Content B
         </div>
         <h4> Next tab: See the Your Offer. </h4>
      </div>
   </li>

</ul>

Jquery for this is

$(document).ready(function(){
    $(".tabs").after("<div class='tabContent'></div>");

    $(".tabs li>a").on("click", function(e){
        var $tab = $(this).parent();
        var tabIndex = $tab.index();

        $tab.parent("ul").find("li").removeClass("active");
        $tab.addClass("active");

        var tabContent = $tab.find(">div").clone(true);
        $(".tabContent").html(tabContent);
        e.preventDefault();
    });

});

The functionality works as intended - clicking on a tab changes it.

Live demo

$(document).ready(function(){
$(".tabs").after("<div class='tabContent'></div>");

$(".tabs li>a").on("click", function(e){
var $tab = $(this).parent();
var tabIndex = $tab.index();

$tab.parent("ul").find("li").removeClass("active");
$tab.addClass("active");

var tabContent = $tab.find(">div").clone(true);
$(".tabContent").html(tabContent);
e.preventDefault();
});

});
.coltab{
height: 51vh;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.tabs {
margin: 0;
padding: 0;
border-top: 1px solid #000;
border-bottom: 1px solid #000;
overflow: hidden;
display: table;
border-collapse: collapse;
border-spacing: 0;
width: 100%;
}
 .tabs li {
display: table-cell;
margin: 0;
padding: 0;
list-style: none;
border-right: 1px solid #000;
}
.tabs li>a {
display: block;
font-weight: bold;
text-align: center;
padding: 5px;
}
.tabs li>a:hover,.tabs li.active a {
background-color: #D3D3D3;
}

.tabs li .content {
display: none;
}

.tabContent {
padding: 15px 15px 0px 15px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="tabs">
   <li class="active">
     [...]
          <h4>Back to first tab</h4>
      </div>
      </div>
      </div>
   </li>
</ul>

Answer №1

You have the option to insert the following code:

$("h4").click(function(){
  var activeTab = $("ul.tabs > li.active");
  var nextTab = (activeTab.is(':last-child') == true ? $("ul.tabs > li:first") : $("ul.tabs > li.active").next("li"));
  nextTab.find("a").trigger("click")
});

This code will enable you to click on the h4 element, which acts as a trigger to move to the next content.

Check out a demo of it in action:

$(document).ready(function(){
$(".tabs").after("<div class='tabContent'></div>");

$(".tabs li>a").on("click", function(e){
var $tab = $(this).parent();
var tabIndex = $tab.index();

$tab.parent("ul").find("li").removeClass("active");
$tab.addClass("active");

var tabContent = $tab.find(">div").clone(true);
$(".tabContent").html(tabContent);
e.preventDefault();
});

  $("h4").click(function(){
    var activeTab = $("ul.tabs > li.active");
    var nextTab = (activeTab.is(':last-child') == true ? $("ul.tabs > li:first") : $("ul.tabs > li.active").next("li"));
    nextTab.find("a").trigger("click")
  });
});
.coltab{
height: 51vh;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.tabs {
margin: 0;
padding: 0;
border-top: 1px solid #000;
border-bottom: 1px solid #000;
overflow: hidden;
display: table;
border-collapse: collapse;
border-spacing: 0;
width: 100%;
}
 .tabs li {
display: table-cell;
margin: 0;
padding: 0;
list-style: none;
border-right: 1px solid #000;
}
.tabs li>a {
display: block;
font-weight: bold;
text-align: center;
padding: 5px;
}
.tabs li>a:hover,.tabs li.active a {
background-color: #D3D3D3;
}

.tabs li .content {
display: none;
}

.tabContent {
padding: 15px 15px 0px 15px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="tabs">
   <li class="active">
      <a href="#">Explainer (2mins)</a>
      <div class="content">
         <div id="Video" class="tabcontent">
            <div class="coltab">
               content A
            </div>
            <h4>Next tab: View Some Sample Lessons</h4>
         </div>
      </div>
      </div>
   </li>
   <li>
      <a href="#">Sample Lessons</a>
      <div class="content coltab">
         <div>
            Content B
         </div>
         <h4> Next tab: See the Your Offer. </h4>
      </div>
   </li>
   <li>
      <a href="#">Special Offer</a>
      <div class="content">
         <div class="coltab">
            Content C
         </div>
         <h4>Next tab: To Register and Subscribe>>></h4>
      </div>
      </div>
   </li>
   <li>
      <a href="#">Subscribe</a>
      <div class="content coltab">
        <div>
         Content D
      </div>
        <div>
      <h4>Next tab: To Request a callback</h4>
      </div>
      </div>
   </li>
   <li>
      <a href="#">Request a Callback</a>
      <div class="content coltab">
         <div>
            Content E
         </div>
         <h4>Next tab: Reviews</h4>
      </div>
      </div>
      </div>
   </li>
   <li>
      <a href="#">Reviews</a>
      <div class="content">
         <div>
            Content F
         </div>
         <h4>Back to first tab</h4>
      </div>
      </div>
      </div>
   </li>
</ul>

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

The use of Array.push() within an $http.get() function in AngularJs results in an array with unexpected

I'm stuck trying to debug my code, particularly because it seems to be related to a javascript issue. The problem arises when I attempt to load a local txt file using $http.get (is there another method that might work better?). The goal is to store t ...

Why is the background image not loading properly on first hover with CSS?

Whenever we load a page with a large image set as the background for a div, there seems to be a delay in displaying the image when we hover over the div. Is there a solution to this issue? I would prefer not to use a sprite image because I need to make alt ...

Discovering the desired element within the added elements in order to trigger a click action

In an attempt to make the pause button function independently in each span element, I am facing a challenge. Whenever the user clicks the "add" button (code not provided), a new set of elements enclosed by <span class="emailform"> is appended to < ...

Efficiently loading Angular views with lazy loading techniques

I am currently working on a calculator website powered by Angular. This single-page site is fully responsive and divided into 7 sections, each featuring different calculators based on user preferences. To improve loading time, I am interested in implementi ...

Empty canvas when Material UI Modal transitions states

I've been struggling to make a simple modal using material UI, but every time I try to change the state, it just shows a blank white page. Can anyone help me figure out why this is happening? Here's the code snippet: import {Button,Modal} fro ...

Puzzle: Ensuring all properties of JavaScript objects within an array are defined

Consider the vue.js object provided below: FormattedData: Object 1: Object approved: true 2: Object approved: undefined 3: Object approved: false 4: Object approved: true Seeking a more effective and concis ...

Leveraging React's UseEffect() to retrieve information from GraphQL

I am currently attempting to retrieve data from graphQL. I understand that by placing a function inside the react UseEffect(), I can trigger the function once the data is updated and structured. However, while working on a chatroom feature, I am facing an ...

My redirect is being disrupted by passing a text string with new lines through GET requests

My issue involves submitting a form that utilizes JavaScript to pass a value through GET. Upon reaching the submission page, the function runs successfully, but when redirecting via the header, it ends up on a blank page. The header code for redirection lo ...

Can the use of a CDN impact the functionality of Intellisense with jQuery?

According to "jQuery for ASP.NET Developers," in order to get Intellisense support in Visual Studio for jQuery, you need to create a VSDoc file with the same name as your JavaScript file but with -vsdoc inserted before the .js file extension. For example, ...

Having trouble keeping the passport authenticated during chai tests

I have been developing tests for routes that require authentication with passport and express-session. In normal circumstances, the routes authenticate successfully and everything functions as expected. However, during testing with chai, the authenticatio ...

a user-friendly database solution for storing data in HTML 5 and Google Drive

Greetings, I am currently faced with the following dilemma: As I work on my angular 2 application, I find myself needing to save certain data. Personally, I have a preference for saving data in JSON format. Here is the scenario: Imagine a todo list where ...

Issue with React MUI V5 causing theme not to display properly within shadow-root element

After trying to attach the shadow root using the MUI createTheme function, I encountered rendering issues. Despite following the instructions in this helpful guide on the MUI website, my problem persists. // App.js const cache = createCache({ key: ' ...

Customize TYPO3 templates by modifying the div id structure

I have a template in TYPO3 that I want to use for multiple pages. Within this template, there is a specific DIV element. I am wondering if it's possible to change the ID of the DIV based on the page UID. This DIV is the only one that changes its cont ...

Iterate over the MVC model and deliver the results

Below is the code snippet for the model that retrieves names and ids... public class AccountType { public int Id { get; set; } public string Name { get; set; } public static AccountType[] AvailableAccountTypes { get { ...

Disabling the visibility of elements through a transparent sticky-top menu

I'm having an issue with my website design. I have a gradient background and a sticky-top menu on the site. The problem is that when I scroll down, the content appears through the menu, which is not ideal. I don't want to apply the same gradient ...

Can someone explain the method for displaying or concealing a menu based on scrolling direction?

https://i.stack.imgur.com/tpDx0.jpg I want to hide this menu when scrolling down and show it when scrolling up. The code for my menu bot is: <script> var previousScroll = 0; $(window).scroll(function(event){ v ...

ReactJs - Reactstrap | Issue with Jumbotron displaying background color

I am trying to create a gray background using Jumbotron in reactstrap. After installation and reference in required places - index.js import 'bootstrap/dist/css/bootstrap.css'; Jumbotron has been implemented in SignIn.js - import Re ...

Controller and Directive both setting up isolated scope for a shared element

Having trouble with adding the ng-intro-options attribute to the HTML element. The Issue var App = angular.module('MyApp', ['infinite-scroll', 'angular-intro']); App.controller('MyController', ['$scope', ...

Eliminate any trace of Bootstrap 4 design elements on the .card-header

I'm facing some difficulties in removing all the default styling of Bootstrap 4 from the .card component. It seems like this issue might not be directly related to Bootstrap since I am also experiencing it in Edge browser. Could someone please assist ...

The Best Way to Calculate a Total Sum with Comma-Separated Values in AngularJS

In my application, I am utilizing the MEAN stack with AngularJS as the front-end. I am trying to figure out how to calculate the total sum and include comma values. I have managed to get the total sum value, but the comma value is not being calculated prop ...