Open a tab by clicking on the navbar link

Asked

Viewed 1,087 times

2

I have a navbar that has several links to the same page that has 3 tab. I need that by clicking on a navbar item it opens the page with the specific tab selected.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

I tried to do so (but it didn’t work):

$("#menu").click(function(){
			$("#dados-capa").removeClass("active");
			$("#itens").addClass("active");
		})

PS. The system is in Laravel, the tabs link is in this format: /data-cover

2 answers

2

Recreating the same situation:

$(document).on('click','.navbar-nav li a',function(){
  var tab = $(this).data('tab');
  $('.'+tab).trigger('click');
  console.log(tab);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<div class="row">
<div class="col-md-12">
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="#">Tabs</a>
    </div>
    <ul class="nav navbar-nav">
      <li><a class="active"  data-tab="tab1" href="#">Tab 1</a></li>
      <li><a href="#" data-tab="tab2">Tab 2</a></li>
      <li><a href="#" data-tab="tab3">Tab 3</a></li>
    </ul>
  </div>
</nav>
</div>
<div class="col-md-12">
<ul class="nav nav-tabs">
  <li class="active tab1"><a data-toggle="tab" href="#tab1">Tab 1</a></li>
  <li><a data-toggle="tab" href="#tab2" class="tab2">Tab 2 1</a></li>
  <li><a data-toggle="tab" href="#tab3" class="tab3">Tab 3 2</a></li>
</ul>
   <div class="tab-content">
  <div id="tab1" class="tab-pane fade in active">
    tab 1
  </div>
  <div id="tab2" class="tab-pane fade">
    tab 2
  </div>
  <div id="tab3" class="tab-pane fade">
    tab 3
  </div>
</div>
</div>
</div>

See that an added data-attr with tab to navbar containing which Nav of tabs must be clicked, after capturing the value, trigera a click in the browser of tab with classname in question.

OBS: console.log() is left for you to see the tab indicated in the example, is not required in the final product.

-1

If you are using jQuery Tabs:

$( "#tabs" ).tabs({ active: #seutab });
  • I am not using Jquery Tabs, I am using bootstrap Nav-tabs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.