Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

96
Visualizações
jQuery multiple button show and hide elements when click

I have a multiple buttons has show and hide class. Which is also activate the elements every toggle click. I want to make it a shorter code and make it globally. Please help me how to do it. All I want is to achieve a lesser code and same with the result.. Thank you.

$('.show').on('click', function () {
    $(this).addClass('inactive');
    $('.hide').removeClass('inactive');
    $('.helloworld').removeClass('inactive')
})
$('.hide').on('click', function () {
    $(this).addClass('inactive');
    $('.show').removeClass('inactive');
     $('.helloworld').addClass('inactive')
})
$('.ok').on('click', function () {
    $(this).addClass('inactive');
    $('.cancel').removeClass('inactive');
    $('.thanks').removeClass('inactive')
})
$('.cancel').on('click', function () {
    $(this).addClass('inactive');
    $('.ok').removeClass('inactive');
     $('.thanks').addClass('inactive')
})
<style>
.inactive{
  display:none;
}
button{
  padding:5px 25px;
  color: #fff;
  background-color:#1d9bf0;
  margin-top: 10px;
}
</style>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="show"> + Show </button>
<button class="hide inactive"> - Hide </button>
<p class="helloworld inactive">Hello WOrld</p>
<br>
<button class="ok"> + Ok </button>
<button class="cancel inactive"> - Cancel </button>
<p class="thanks inactive">Thank you</p>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The technique you're looking for here is DRY, or Don't Repeat Yourself. To do this, look for the common patterns in the logic you have.

In this case each button has its text updated, and it changes the state of it's following sibling. Therefore you can place common class attributes on the elements so that the same JS logic can be applied to them all. From there you can use jQuery's DOM traversal methods to relate the elements to each other, and also data attributes to store custom metadata about the elements which can be used when the click event occurs.

Finally you can use toggleClass() to add/remove the classes to display/hide the elements as necessary.

Here's a working example:

$('.toggle').on('click', e => {
  let $btn = $(e.target);
  $btn
    .text(() => $btn.data($btn.hasClass('show') ? 'hide-text' : 'show-text')).toggleClass('show') // update text
    .next().toggleClass('inactive'); // toggle related content
})
<style>
  .inactive {
    display: none;
  }
  
  button {
    padding: 5px 25px;
    color: #fff;
    background-color: #1d9bf0;
    margin-top: 10px;
  }
</style>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="toggle-container">
  <button class="toggle show" data-show-text="+ Show" data-hide-text="- Hide">+ Show</button>
  <p class="content inactive">Hello WOrld</p>
</div>
<div class="toggle-container">
  <button class="toggle show" data-show-text="+ Ok" data-hide-text="- Cancel">+ Ok</button>
  <p class="content inactive">Thank you</p>
</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda