Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

Showing Previous and Next Tab Divs with JavaScript and jQuery

Posted on November 13, 2019August 4, 2020 by Habibie

Here is another example and tutorial how we can do a show and hide previous and next HTML5 divs/tabs based on it’s id’s and classes…

The idea is simple, we must store a value of a number of current tab that we are seeing right now in a variable, then by clicking next button we show the next tab with that value+1, and if we click previous button we show the tab with that stored value-1, also with hiding the rest unnecessary tabs.

Check out thsi code:

<!DOCTYPE html>
<html>
    <head>
        <title>Tabs</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta http-equiv="X-UA-compatible" content="ie=edge">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <style>
            .tabcontent { display: none;}
            .tabcontent .active{display: block; display: show ;}
        </style>
    </head>
    <body>
        <div class="container text-center">
            <h1>Tabs with buttons</h1>
            <button class="float-left btn bg-primary" onclick="prev()">Previous </button>
            <button class="btn btn-primary float-right" onclick="next()">Next</button>
            <nav class="multiTabs">
                <a class="btn btn-danger mr-2" href="javascript:void(0)" data-trigger='content1'>Tab1</a>
                <a class="btn btn-danger mr-2" href="javascript:void(0)" data-trigger='content2'>Tab2</a>
                <a class="btn btn-danger mr-2" href="javascript:void(0)" data-trigger='content3'>Tab3</a>
                <a class="btn btn-danger mr-2" href="javascript:void(0)" data-trigger='content4'>Tab4</a>
                <a class="btn btn-danger mr-2" href="javascript:void(0)" data-trigger='content5'>Tab5</a>
            </nav>
            
            
            <!-- content  -->
            <div class="tabcontent active bg-light" id="content1">
                <h1>Tab1</h1>
                <p>This is some text about tab1</p>
            </div>
            <div class="tabcontent bg-light" id="content2">
                <h1>Tab2</h1>
                <p>This is some text about tab2</p>
            </div>
            <div class="tabcontent  bg-light" id="content3">
                <h1>Tab3</h1>
                <p>This is some text about tab3</p>
            </div>
            <div class="tabcontent  bg-light" id="content4">
                <h1>Tab4</h1>
                <p>This is some text about tab4</p>
            </div>
            <div class="tabcontent bg-light" id="content5">
                <h1>Tab5</h1>
                <p>This is some text about tab5</p>
            </div>
        </div>
        <script>
			var currentTab = 1;
			$("#contetn1").show();
		
            $(document).on('click', 'nav.multiTabs>a',
			function(){
				var TabId = $(this).attr('data-trigger');
				$('div#'+TabId+' ').show();
				
				console.log("Current Tab: " + TabId);
				currentTab = parseInt(TabId.replace("content", ""));

				$('.tabcontent:not(#'+TabId+')').hide()

            });
			
			function next(){
				if(currentTab < 5){
					$(".tabcontent").hide();
					currentTab++;
					$("#content"+(currentTab)).show();
				}
			}
			
			function prev(){
				if(currentTab > 1){
					$(".tabcontent").hide();
					currentTab--;
					$("#content" + (currentTab)).show();
				}
			}
        </script>
    </body>
</html>
        

To see the details please watch this video and leave your comments 😀

Post Views: 536
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Hover Reveal script for Unity to show and hide object on mouse hover
  • How to Prevent UI Clicks in Unity from “Bleeding Through” to 3D Objects Behind Them
  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
© 2026 ThirteeNov | Powered by Superbs Personal Blog theme