< Back to Code

Tab change on click and detect hashtag changed

This simple code help you to create tab buttons and tab contents

When the user clicks on the tab button, the tab content will reveal accordingly

The simple code use jquery and useful when you need to display lots of content in different tabs

					<div style="padding: 20px">
    <a class="btn" id="btn1" href="#btn-1">Button 1</a>
    <a class="btn" id="btn2" href="#btn-2">Button 2</a>
    <a class="btn" id="btn3" href="#btn-3">Button 3</a>

    <div class="tab" id="tab1">tab content 1</div>
    <div class="tab" id="tab2">tab content 2</div>
    <div class="tab" id="tab3">tab content 3</div>

$( document ).ready(function() {
    var hash="btn-1";

$(window).bind('hashchange load', function () { 
if(window.location.hash) {
    hash = window.location.hash.substring(1);
    if (hash == "btn-1"){
        $(".btn, .tab").removeClass("selected");
        $("#btn1, #tab1").addClass("selected");
    } else if(hash == "btn-2"){
        $(".btn, .tab").removeClass("selected");
        $("#btn2, #tab2").addClass("selected");     
    } else if(hash == "btn-3"){
        $(".btn, .tab").removeClass("selected");
        $("#btn3, #tab3").addClass("selected"); 



border: 1px solid;
padding: 10px;

        background: red;
        background: #ccc;
        padding: 100px;
        opacity: 0.5;
        opacity: 1;
content tab 1
content tab 2
content tab 3


© Copyright by JAYbranding – All rights reserved.

We would like to advise you in more detail.
Please enter information to receive a consultation