skip to Main Content

I am using wordpress and want to use nav-tab in bootstrap library.

The default nav-tab mode of the Bootstrap library is very simple, so I want to improve it with a little script code.

I want the first tab to be active when the page is loaded and its background to be different from the rest of the tabs. But when another tab is clicked, the new tab will be activated and its background will change, and the first tab will take the default background and become inactive.

I wrote a separate script for each tab, which is very bad, and worse, this code does not work at all. That is, by clicking on each tab instead of the background (li); The background (ul) changes.

       $('.nav_main').find('li.content-desc').click(function () {
            if ($(this).parent().find("ul").eq(0).is(':visible')) {
                $(this).parent().removeClass('active');
            } else {
                $(this).parent().addClass('active');
            }
        });

        $('.nav_main').find('li.content-author').click(function () {
            if ($(this).parent().find("ul").eq(0).is(':visible')) {
                $(this).parent().removeClass('active');
            } else {
                $(this).parent().addClass('active');
            }
        });

        $('.nav_main').find('li.content-comment').click(function () {
            if ($(this).parent().find("ul").eq(0).is(':visible')) {
                $(this).parent().removeClass('active');
            } else {
                $(this).parent().addClass('active');
            }
        });

        $('.nav_main').find('li.content-tag').click(function () {
            if ($(this).parent().find("ul").eq(0).is(':visible')) {
                $(this).parent().removeClass('active');
            } else {
                $(this).parent().addClass('active');
            }
        });
.content_main {
    width: 650px;
    border: 1px solid #00000033;
    text-align: justify;
    margin: 0px 5px 10px 5px;
    padding: 20px;
    border-radius: 5px;
    line-height: 35px;
}

.nav_main ul.active {
    background: #b421f3;
}

.content-tab {
    border: none;
}

.nav-tabs>li>a {
    background: #e2e2e2;
    border: 1px solid #00000033;
    box-shadow: 0 -2px 0 #2196f3;
    padding: 14px;
    color: #000;
    margin: 0px 15px 0px 5px;
    border-radius: 5px 5px 0 0;
}

.content-tab {
    margin-top: 20px;
}

.content-tab a:hover {
    text-decoration: none;
}

.content-comment {
    margin-right: -10px;
}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>



<div class="container">
        <div class="nav_main">
            <!-- Nav tabs -->
            <ul class="nav nav-tabs content-tab" role="tablist">
                <li role="presentation" class="content-desc"><a class="desc" href="#desc" aria-controls="desc"
                        role="tab" data-toggle="tab">content</a></li>
                <li role="presentation" class="content-author"><a class="author" href="#author" aria-controls="author"
                        role="tab" data-toggle="tab">author</a></li>
                <li role="presentation" class="content-comment"><a class="comment" href="#comment"
                        aria-controls="comment" role="tab" data-toggle="tab">comment</a></li>
                <li role="presentation" class="content-tag"><a class="tag" href="#tag" aria-controls="tag" role="tab"
                        data-toggle="tab">tag</a></li>
            </ul>
            <!-- Tab panes -->
            <div class="tab-content content_main">
                <div role="tabpanel" class="tab-pane active" id="desc">
                    desc desc desc desc desc desc desc
                </div>
                <div role="tabpanel" class="tab-pane" id="author">
                    author author author author author
                </div>
                <div role="tabpanel" class="tab-pane" id="comment">
                    comment comment comment comment comment comment
                </div>
                <div role="tabpanel" class="tab-pane" id="tag">
                    tag tag tag tag tag tag
                </div>
            </div>
        </div>

    </div>

2

Answers


  1. You can keep only one click event for all tabs and inside that using $(this) and id show or hide your tabs.

    Demo Code :

    $("#myTab li > a ").click(function(e) {
      e.preventDefault();
      $("#myTab li > a ,.tab-pane").removeClass("active");
      $(this).addClass("active")
      $(".tab-pane").hide() //hide all other tabs
      $(".tab-content " + $(this).attr("href")).addClass("active").show(); //show curent 
    })
    
    $('#myTab > li:eq(0) a').click() // trigger click on first a
    .content_main {
      width: 650px;
      border: 1px solid #00000033;
      text-align: justify;
      margin: 0px 5px 10px 5px;
      padding: 20px;
      border-radius: 5px;
      line-height: 35px;
    }
    
    .nav_main .active {
      background: #b421f3;
    }
    
    .content-tab {
      border: none;
    }
    
    .nav-tabs>li>a {
      background: #e2e2e2;
      border: 1px solid #00000033;
      box-shadow: 0 -2px 0 #2196f3;
      padding: 14px;
      color: #000;
      margin: 0px 15px 0px 5px;
      border-radius: 5px 5px 0 0;
    }
    
    .content-tab {
      margin-top: 20px;
    }
    
    .content-tab a:hover {
      text-decoration: none;
    }
    
    .content-comment {
      margin-right: -10px;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    
    
    
    
    <div class="container">
      <div class="nav_main">
        <!-- Nav tabs -->
        <ul class="nav nav-tabs content-tab" role="tablist" id="myTab">
          <li role="presentation" class="content-desc"><a class="desc active" href="#desc" aria-controls="desc" role="tab" data-toggle="tab">content</a></li>
          <li role="presentation" class="content-author"><a class="author" href="#author" aria-controls="author" role="tab" data-toggle="tab">author</a></li>
          <li role="presentation" class="content-comment"><a class="comment" href="#comment" aria-controls="comment" role="tab" data-toggle="tab">comment</a></li>
          <li role="presentation" class="content-tag"><a class="tag" href="#tag" aria-controls="tag" role="tab" data-toggle="tab">tag</a></li>
        </ul>
        <!-- Tab panes -->
        <div class="tab-content content_main">
          <div role="tabpanel" class="tab-pane active" id="desc">
            desc desc desc desc desc desc desc
          </div>
          <div role="tabpanel" class="tab-pane" id="author">
            author author author author author
          </div>
          <div role="tabpanel" class="tab-pane" id="comment">
            comment comment comment comment comment comment
          </div>
          <div role="tabpanel" class="tab-pane" id="tag">
            tag tag tag tag tag tag
          </div>
        </div>
      </div>
    
    </div>

    Other way : You can enable each tab via click event and initialise your tabs using bootstrap.Tab() and can make the first tab active by calling .click() on it .

    Demo Code :

    const triggerTabList = document.querySelectorAll('#myTab a')
    triggerTabList.forEach(triggerEl => {
      const tabTrigger = new bootstrap.Tab(triggerEl)
    
      triggerEl.addEventListener('click', event => {
        event.preventDefault()
        tabTrigger.show()
      })
    })
    
    document.querySelector('#myTab > li:first-child a').click()
    .content_main {
      width: 650px;
      border: 1px solid #00000033;
      text-align: justify;
      margin: 0px 5px 10px 5px;
      padding: 20px;
      border-radius: 5px;
      line-height: 35px;
    }
    
    .nav_main .active {
      background: #b421f3;
    }
    
    .content-tab {
      border: none;
    }
    
    .nav-tabs>li>a {
      background: #e2e2e2;
      border: 1px solid #00000033;
      box-shadow: 0 -2px 0 #2196f3;
      padding: 14px;
      color: #000;
      margin: 0px 15px 0px 5px;
      border-radius: 5px 5px 0 0;
    }
    
    .content-tab {
      margin-top: 20px;
    }
    
    .content-tab a:hover {
      text-decoration: none;
    }
    
    .content-comment {
      margin-right: -10px;
    }
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
    
    
    
    <div class="container">
      <div class="nav_main">
        <!-- Nav tabs -->
        <ul class="nav nav-tabs content-tab" role="tablist" id="myTab">
          <li role="presentation" class="content-desc"><a class="desc" href="#desc" aria-controls="desc" role="tab" data-toggle="tab">content</a></li>
          <li role="presentation" class="content-author"><a class="author" href="#author" aria-controls="author" role="tab" data-toggle="tab">author</a></li>
          <li role="presentation" class="content-comment"><a class="comment" href="#comment" aria-controls="comment" role="tab" data-toggle="tab">comment</a></li>
          <li role="presentation" class="content-tag"><a class="tag" href="#tag" aria-controls="tag" role="tab" data-toggle="tab">tag</a></li>
        </ul>
        <!-- Tab panes -->
        <div class="tab-content content_main">
          <div role="tabpanel" class="tab-pane active" id="desc">
            desc desc desc desc desc desc desc
          </div>
          <div role="tabpanel" class="tab-pane" id="author">
            author author author author author
          </div>
          <div role="tabpanel" class="tab-pane" id="comment">
            comment comment comment comment comment comment
          </div>
          <div role="tabpanel" class="tab-pane" id="tag">
            tag tag tag tag tag tag
          </div>
        </div>
      </div>
    
    </div>
    Login or Signup to reply.
  2. you can make thit without Jquery

    .tab-content {
        max-width: 650px;
        border: 1px solid #00000033;
        text-align: justify;
        margin: 0px 5px 10px 5px;
        padding: 20px;
        border-radius: 5px;
        line-height: 35px;
    }
    
    .nav-link.active {
        background-color: #b421f3 !important;
    }
    
    .nav-tabs {
        border: none;
    }
    
    .nav-tabs > li > .nav-link {
        background: #e2e2e2;
        border: 1px solid #00000033;
        box-shadow: 0 -2px 0 #2196f3;
        padding: 14px;
        color: #000;
        margin: 0px 15px 0px 5px;
        border-radius: 5px 5px 0 0;
    }
    
    .nav-tabs {
        margin-top: 20px;
    }
    
    .nav-link:hover {
        text-decoration: none;
    }
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js
    "></script>
    
    <div class="container">
      <div class="nav_main">
        <!-- Nav tabs -->
        <ul class="nav nav-tabs" role="tablist">
          <li class="nav-item" role="presentation">
            <button class="nav-link active" id="content-tab" data-bs-toggle="tab" data-bs-target="#content" type="button" role="tab" aria-controls="content" aria-selected="true">content</button>
          </li>
          <li class="nav-item" role="presentation">
            <button class="nav-link" id="author-tab" data-bs-toggle="tab" data-bs-target="#author" type="button" role="tab" aria-controls="author" aria-selected="false">author</button>
          </li>
          <li class="nav-item" role="presentation">
            <button class="nav-link" id="comment-tab" data-bs-toggle="tab" data-bs-target="#comment" type="button" role="tab" aria-controls="comment" aria-selected="false">comment</button>
          </li>
          <li class="nav-item" role="presentation">
            <button class="nav-link" id="tag-tab" data-bs-toggle="tab" data-bs-target="#tag" type="button" role="tab" aria-controls="tag" aria-selected="false">tag</button>
          </li>
        </ul>
                    
        <!-- Tab panes -->
        <div class="tab-content">
          <div class="tab-pane fade show active" id="content" role="tabpanel" aria-labelledby="content-tab" tabindex="0">
            desc desc desc desc desc desc desc
          </div>
          <div class="tab-pane fade" id="author" role="tabpanel" aria-labelledby="author-tab" tabindex="0">
            author author author author author
          </div>
          <div class="tab-pane fade" id="comment" role="tabpanel" aria-labelledby="comment-tab" tabindex="0">
            comment comment comment comment comment comment
          </div>
          <div class="tab-pane fade" id="tag" role="tabpanel" aria-labelledby="tag-tab" tabindex="0">
            tag tag tag tag tag tag
          </div>
        </div>
      </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search