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

150
Visualizações
Is there a more efficient way to change the style of the clicked buttons?

I have about 40 button on the project but my example shows only four. My code is very repetitive and wondering if there's a lighter approach or language? I'd like to hide or change style of the clicked button/s.

$("[data-id='1']").on('click', function() {
  $("[data-id='1']").css('visibility', 'hidden');
});
$("[data-id='2']").on('click', function() {
  $("[data-id='2']").css('visibility', 'hidden');
});
$("[data-id='3']").on('click', function() {
  $("[data-id='3']").css('visibility', 'hidden');
});
$("[data-id='4']").on('click', function() {
  $("[data-id='4']").css('visibility', 'hidden');
});
button {
  margin: 20px 0 0 20px;
  padding: 5px 30px
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button data-id='1' class="hope">1</button>
<button data-id='2' class="hope">2</button>
<button data-id='3' class="hope">3</button>
<button data-id='4' class="hope">4</button>

Thanks http://jsfiddle.net/05cempj7/

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

This function operates on all elements having the .hope class.

$(".hope").on('click', function() {
  $(this).css('visibility', 'hidden');
});

If clicked on any hope class $(".hope") then $(this) reefers to the element that is clicked on.

about 4 years ago · Santiago Gelvez 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