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

What is the most efficient way to dynamically alter the position of a div element within an HTML document using

In a scenario where a parent div contains multiple child divs, it is required that one particular child div named .div_object_last always stays at the end. Below is an example setup : HTML <div class="div_parent"> <div class="div_object"> ...

What is the technique for adding a blur effect to the top and right side of a div element?

Here is a link to my fiddle: https://jsfiddle.net/aod4rmx8/ .background{ position: absolute; left: 0; width:50%; right: 0; bottom: 0; top:0; background-color: #000; /*box-shadow: 100px 0 40px 6px rgba(0, 0, 0, 1);*/ -webk ...

Error: Unable to access undefined properties while trying to read 'add' value. Issue identified in the code related to "classlist.add" function

I am currently facing an issue where I receive the error message: Uncaught TypeError: Cannot read properties of undefined (reading 'add') when trying to add a class to a div using a button. After researching on Stack Overflow, I stumbled upon a ...

Unable to adjust the top padding of the navbar to 0 pixels

Having recently started learning HTML and CSS, I am encountering an issue with the padding on my navbar. I am unable to get it to align with the top of the site as there is a persistent space between the navbar and the top. Below is my HTML code: <!do ...

Guide to showcasing console output on a Web Server

Apologies if this question is not the most suitable for this platform. I recently set up Pure-FTPd service on a CentOS server. To check current connections, I use the command pure-ftpwho, which gives me the following output: +------+---------+-------+---- ...

Unable to retrieve Objects from JSON

Here is a JSON object I received: [{"model": "pricing.cashflow", "pk": 1, "fields": {"value": 4.0, "date": "2016-09-09"}}, {"model": "pricing.cashflow", "pk": 2, "fields": {"value": 3.0, "date": "2016-09-01"}}, {"model": "pricing.cashflow", "pk": 3, "fiel ...

Can a JavaScript function be sent through a REST API using Node.js and Express?

I have a unique scenario where I need to send a custom JavaScript function as a response to a GET request made to an API endpoint. Currently, I am using Node.js with Express for my server implementation, but I am open to exploring other frameworks. When a ...

Processing ajax requests in Rails 4 using HTML format

I'm currently in the process of setting up ajax functionality for my contact form and I am currently testing to ensure that the ajax call is being made. When checking my console, I noticed that it is still being processed as HTML and I cannot seem to ...

The webpage fails to return to its original position after the script has been executed

My website has a sticky div that stays at the top when scrolling down, but does not return to its original position when scrolling back up. Check out this example function fixDiv() { var $div = $("#navwrap"); if ($(window).scrollTop() > $div.data("top ...

In need of a collection of modules determined by a DefinePlugin constant

Currently, I am in the process of constructing a web application utilizing Webpack. However, I have encountered a challenge with a particular aspect of the design - hopefully someone in this forum has experience in similar tasks (or possesses enough knowle ...

Using Array.push to add an object retrieved from a Redis cache in a Node.js application is causing issues and is not functioning as expected

I've encountered a problem with retrieving all keys from my Redis cache, storing them in an array, and sending that array to the user using Express. The issue arises when I receive an empty array as the response with no objects in it. I attempted to u ...

Displaying elements upon checkbox selection in React

Hello, I'm new to React and I am facing an issue with checkbox click handling in React. My goal is to display a div when a checkbox is checked and hide the div when the checkbox is unchecked. The current implementation only shows the div when the che ...

navigate to a different section on the page with a series of visuals preceding it

When I try to navigate to a specific dom element on another page, the page initially works fine but then jumps to somewhere before that element. I believe this issue occurs because the page calculates the position before images load, and once the images ar ...

What's the Best Way to Add a Border to My Two-Column Form?

I'm currently working on a reservation form for my website with a 2-column layout. In one column, I have tables, and I've been trying to add a divider between the columns but haven't found a solution yet. Despite researching ways to add a d ...

What is the method of posting to PHP using JavaScript without utilizing Ajax?

My current code involves a drag and drop file uploader to PHP using ajax, but it seems that my web host does not support ajax. Is there an alternative method to achieve this without using ajax? Specifically, I am facing issues with the UploadFile functio ...

Utilize a function or array to send various data with an Ajax post request

Hey, I'm on the hunt for a more efficient method to send data using ajax to php for multiple users. Take a peek at my code below: $(document).ready(function(){ $("#all").click(function(){ document.getElementById('babon').click(); ...

Exploring the application of the PUT method specific to a card ID in vue.js

A dashboard on my interface showcases various cards containing data retrieved from the backend API and stored in an array called notes[]. When I click on a specific card, a pop-up named updatecard should appear based on its id. However, I am facing issues ...

Display an error message when the button is clicked and the input field is left empty in a Vue 3 script setup

Hello, I am currently exploring Vue 3 and embarking on a new Vue 3 project venture. However, I seem to be encountering a challenge when it comes to displaying an error message if the button is clicked while the input field remains empty in my Vue 3 script ...

Move a 'square' to a different page and display it in a grid format after clicking a button

I am currently developing a project that allows students or schools to add projects and search for collaborators. On a specific page, users can input project details with a preview square next to the fields for visualization. Once the user uploads the ...

Utilize Vue.js functions within data through the Vue.js context

I'm trying to incorporate a function as a data property. While it works for the 'works' data property, I need access to the this context within the function in order to calculate values from the shoppingCart property. Is there a way to achie ...