Activate button according to page

0

I have a set of divs below the header (cardiac, abdominal, facial ...), each with its respective link, using the same names. I need when I select one of the buttons, it becomes "active" even with the page exchange, because each button has its own page. Ex: I am on the "Facials" page, it is active, below I have a Collapse list with some content, with the first item also active, I click on "Vocals" next button, the page would be changed, the "Vocals" button and the first item in the list below would be active, I am using two JS scripts, one to activate the buttons and the collapse, and another to hide a collapse and show another one depending on the selected item in the list, thank you for help!

<div id="botao-cardiacas-2">
	<a class="link1" href="http://localhost/hospital-iphone/cardiacas/">
	<img class="especialidades-padding img-container" src="http://localhost/hospital-iphone/wp-content\themes\hospitaldoiphone\img\cardiacas.png"/><pclass="texto-especialidades-2">
		Cardíacas
	</p>
	<p class="texto-especialidades-sub-2">
		Problemas com bateria
	</p>
	</a>
</div>

<div id="botao-abdominais-2">
	<a class="link1" href="http://localhost/hospital-iphone/cardiacas/">
	<img class="img-container" src="http://localhost/hospital-iphone/wp-content\themes\hospitaldoiphone\img\abdominais.png"/><pclass="texto-especialidades-2">
		Abdominais
	</p>
	<p class="texto-especialidades-sub-2">
		Problemas com botão Home
	</p>
	</a>
</div>

<div id="botao-faciais-2">
	<a class="link1" href="http://localhost/hospital-iphone/cardiacas/">
	<img class="img-container" src="http://localhost/hospital-iphone/wp-content\themes\hospitaldoiphone\img\faciais.png"/><pclass="texto-especialidades-2">
		Faciais
	</p>
	<p class="texto-especialidades-sub-2">
		Problemas com a tela
	</p>
	</a>
</div>

<div id="botao-vocais-2">
	<a class="link1" href="http://localhost/hospital-iphone/cardiacas/">
	<img class="img-container" src="http://localhost/hospital-iphone/wp-content\themes\hospitaldoiphone\img\vocais.png"/><pclass="texto-especialidades-2">
		Vocais
	</p>
	<p class="texto-especialidades-sub-2">
		Falantes e microfones
	</p>
	</a>
</div>

<div id="botao-estetica-2">
	<a class="link1" href="http://localhost/hospital-iphone/cardiacas/">
	<img class="img-container" src="http://localhost/hospital-iphone/wp-content\themes\hospitaldoiphone\img\estetica.png"/><pclass="texto-especialidades-2">
		Estética
	</p>
	<p class="texto-especialidades-sub-2">
		Tampa traseira
	</p>
	</a>
</div>

<div id="botao-cognitiva-2">
	<a class="link1" href="http://localhost/hospital-iphone/cardiacas/">
	<img class="especialidades-padding img-container" src="http://localhost/hospital-iphone/wp-content\themes\hospitaldoiphone\img\cognitiva.png"/><pclass="texto-especialidades-2">
		Cognitiva
	</p>
	<p class="texto-especialidades-sub-2">
		Antenas Wifi e GSM
	</p>
	</a>
</div>

<div id="botao-visuais-2">
	<a class="link1" href="http://localhost/hospital-iphone/cardiacas/">
	<img class="especialidades-padding img-container" src="http://localhost/hospital-iphone/wp-content\themes\hospitaldoiphone\img\visual.png"/><pclass="texto-especialidades-2">
		Visuais
	</p>
	<p class="texto-especialidades-sub-2">
		Câmeras
	</p>
	</a>
</div>

div id="botao-respiratorio-2">
	<a class="link1" href="http://localhost/hospital-iphone/cardiacas/">
	<img class="especialidades-padding img-container" src="http://localhost/hospital-iphone/wp-content\themes\hospitaldoiphone\img\respiratorio.png"/><pclass="texto-especialidades-2">
		Respiratório
	</p>
	<p class="texto-especialidades-sub-2">
		Dados e sistema operacional
	</p>
	</a>
</div>

<div id="botao-outros">
	<a class="link1" href="http://localhost/hospital-iphone/cardiacas/">
	<img class="img-container" src="http://localhost/hospital-iphone/wp-content\themes\hospitaldoiphone\img\outros.png"/><pclass="texto-especialidades-2">
		Outros
	</p>
	<p class="texto-especialidades-sub-2">
		Outros serviços
	</p>
	</a>
</div>

$( document ).ready(function() {
    $('.list-left').click(function() {
        $(this).closest('ul').find('li').removeClass('selected');
        $(this).addClass('selected');
    });
});

$( document ).ready(function() {
    $('#botao-cardiacas-2').click(function() {
        $(this).closest('ul').find('li').removeClass('selected');
        $(this).addClass('selected');
    });
});

$(document).ready(function() {
function id( el ){
        return document.getElementById( el );
}
function hide_all(){
        var divs = id('content1').getElementsByTagName('div');
        for( var i=0; i<divs.length; i++ )
        {
                divs[i].style.display = 'none';
        }
}
/* http://www.javascriptkit.com/jsref/event.shtml */
function disablelink( e ){
        var evt = window.event || e
        if (evt.preventDefault) //supports preventDefault?
                evt.preventDefault()
        else //IE browser
                return false
}
window.onload = function(){
        hide_all();
        var as = id('content1').getElementsByTagName('a');
        for( var i=0; i<as.length; i++ )
        {
                as[i].onclick = function( e ){
                        hide_all();
                        var id_el = this.href.split('#')
                       
                        id( id_el[1] ).style.display = 'block';                
                        return disablelink( e );
                }
        }
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="content1" class="container-content container-height">
<ul id="test-content" class="no-padding">
<li class="list-left list1" ><a href="#item1">Reposição LCD</a></li>        
<div id="item1" class="item1">
	<p class="titulo">Reposição LDC</p>
	<p class="sub-titulo">Cirurgia facial</p>
	<p class="texto">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.</p>
<img scr="#"/>
</div>
	
<li class="list-left list2"><a href="#item2">Reposição TOUCH</a></li>	
<div id="item2" class="item2">
	<p class="titulo">Reposição TOUCH</p>
	<p class="sub-titulo">Cirurgia facial</p>
	<p class="texto">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariaore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.</p>
</div>
	
<li class="list-left list3"><a href="#item3">Reconexão Display / Frontal</a></li>
<div id="item3" class="item3">
	<p class="titulo">Reposição Display</p>
	<p class="sub-titulo">Cirurgia facial</p>
	<p class="texto">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in , sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.</p>
</div>
	
<li class="list-left list4"><a href="#item4">Montagem MO</a></li>	
<div id="item4" class="item4">
	<p class="titulo">Montagem MO</p>
	<p class="sub-titulo">Cirurgia facial</p>
	<p class="texto">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.</p>
</div>
	
<li class="list-left list5"><a href="#item5">Reposição Display Frontal</a></li>
<div id="item5" class="item5">
	<p class="titulo">Reposição Display</p>
	<p class="sub-titulo">Cirurgia facial</p>
	<p class="texto">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.</p>
</div>
	
<li class="list-left list6"><a href="#item6">Aplicação Peliculas</a></li>
<div id="item6" class="item6">
	<p class="titulo">Aplicação de Peliculas</p>
	<p class="sub-titulo">Cirurgia facial</p>
	<p class="texto">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.</p>
</div>
	
</ul>
</div>
    
asked by anonymous 15.03.2018 / 20:02

1 answer

0

If there is no page reloading, you can do this by simply adapting the example to your need. I recommend using the bootstrap to get the desired accordion effect. Involve your% of content% in divs and make panel-group , on the buttons, you will put accordion of id which will be the targets for the effect.

To learn more, see here the bootstrap documentation on accordions.

$('button').click( function(){
    var alvo = $(this); //captura o botao clicado
    $('button').removeClass('ativo') // remove a classe dos demais botoes
    alvo.addClass('ativo');// adiciona a classe ao botao alvo
    
})
.ativo {
   background-color: red
}
<button class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#alvo1" aria-expanded="false" > Exemplo botão 1 </button>

<button class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#alvo2" aria-expanded="false" > Exemplo botão 2 </button>
<button class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#alvo3" aria-expanded="false"> Exemplo botão 3 </button>
<button class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#alvo4" aria-expanded="false"> Exemplo botão 4 </button>


<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">  
    
  <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingTwo">
      <h4 class="panel-title">
        
          Eu sou o alvo 1
        </a>
      </h4>
    </div>
    <div id="alvo1" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
      <div class="panel-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
      </div>
    </div>
  </div>
  <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingThree">
      <h4 class="panel-title">
        
          Eu sou o alvo 2
       
      </h4>
    </div>
    <div id="alvo2" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
      <div class="panel-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
      </div>
    </div>
  </div>
   <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingThree">
      <h4 class="panel-title">
        
          Eu sou o alvo 3
       
      </h4>
    </div>
    <div id="alvo3" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
      <div class="panel-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
      </div>
    </div>
  </div>
   <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingThree">
      <h4 class="panel-title">
        
          Eu sou o alvo 4
       
      </h4>
    </div>
    <div id="alvo4" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
      <div class="panel-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
      </div>
    </div>
  </div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><linkrel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"> </script>

I left the code commented, if in doubt you can ask.

    
15.03.2018 / 22:20