Home » Jquery » jquery – Bootstrap 4 Change Tab on Hover

jquery – Bootstrap 4 Change Tab on Hover

Posted by: admin February 22, 2020 Leave a comment

Questions:

Here is the Bootstrap 4 Tab Default tab structure, How to toggle between the tabs on mouse hover? someone kindly add solution using jquery or javascript.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />


<ul class="nav nav-tabs" id="myTab" role="tablist">
  <li class="nav-item">
    <a class="nav-link active" id="home-tab" data-toggle="tab" href="#home">Home</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" id="profile-tab" data-toggle="tab" href="#profile">Profile</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" id="contact-tab" data-toggle="tab" href="#contact">Contact</a>
  </li>
</ul>
<div class="tab-content" id="myTabContent">
  <div class="tab-pane fade show active" id="home" role="tabpanel">Home</div>
  <div class="tab-pane fade" id="profile" role="tabpanel">Profile</div>
  <div class="tab-pane fade" id="contact" role="tabpanel">Contact</div>
</div>
How to&Answer:

Try this.

$(".nav-link").hover(function(){
    $(this).tab('show');
});

Answer:

This will help you you just need to trigger the click on hover function

$(".nav-link").hover(function(){
$(this).click();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />


<ul class="nav nav-tabs" id="myTab" role="tablist">
  <li class="nav-item">
    <a class="nav-link active" id="home-tab" data-toggle="tab" href="#home">Home</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" id="profile-tab" data-toggle="tab" href="#profile">Profile</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" id="contact-tab" data-toggle="tab" href="#contact">Contact</a>
  </li>
</ul>
<div class="tab-content" id="myTabContent">
  <div class="tab-pane fade show active" id="home" role="tabpanel">Home</div>
  <div class="tab-pane fade" id="profile" role="tabpanel">Profile</div>
  <div class="tab-pane fade" id="contact" role="tabpanel">Contact</div>
</div>