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

104
Visualizações
Improve my show hide divs function Jquery

I'm new and I just made one of my first ugly function to show and hide. But I would like someone that actually knows about the subject to review it and show me a better way to do it.

Basically I'm making a small single page web that has four buttons, and from there you can go to all the sections, its really small content so I choose to switch between divs that are hidden. I'm reading about jquery and been using it lately just a tiny bit. I made a function that works how I want, but I'm sure it can be improved and as result ill learn more.

Thanks!!

Css:

#page1, #page2{
   display: none;
                 }

Jquery:

$(document).ready(function(){
 $("button.page1").click(function(){
  $("#main").hide();
  $("#page1").show();
 });
 $("button.page2").click(function(){
  $("#main").hide();
  $("#page2").show();
 });
 $("button.page3").click(function(){
   alert("Time flies, coming soon!!");
 });
 $("button.page4").click(function(){
   alert("I'm working on it, coming soon!!");
 });
 $("button.gobackfrompage1").click(function(){
  $("#main").show();
  $("#page1").hide();
 });
 $("button.gobackfrompage2).click(function(){
  $("#main").show();
  $("#page2").hide();
 });
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can condense this by adding a class to each button. Then in the jQuery you can bind a click event handler to the class and use the "this" keyword to execute the function on the specific HTML element.

Here's an example: Jquery select this + class

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