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

264
Visualizações
How to make button with two functions using if else statement

I'm trying to make one button that has two functions whether it has class in it or not when clicked. So I tried to make javascript code using if else statement. I don't see what the problem is, but the button is not working..

$(document).ready(function() {
  $(".btn-ctrl").click(function() {
    if ($(".btn-ctrl > span").hasClass("close")) {
      function popDown() {
        $("#banner").slideDown(400);
        $(".btn-ctrl > span").removeClass("close");
      }
    } else {
      function popUp() {
        $("#banner").slideUp(400);
        $(".btn-ctrl > span").addClass("close");
      }
    }
  })
})

.btn-ctrl is class name for my button and I'm changing the button's design by adding and removing class="close" in span tag. I can't figure out what is causing problem

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

0

You create functions inside the condition instead of calling them. Blame functions outside the condition block and then call them or remove them altogether. And before you write anything, read the syntax of the language.

$(document).ready(function() {
    $(".btn-ctrl").click(function() {
        console.log('click')
        if ($(".btn-ctrl > span").hasClass("close")) {
            $("#banner").slideDown(400);
            $(".btn-ctrl > span").removeClass("close");
        } else {
            $("#banner").slideUp(400);
            $(".btn-ctrl > span").addClass("close");
        }
    })
})

or

function popDown() {
    $("#banner").slideDown(400);
    $(".btn-ctrl > span").removeClass("close");
}
function popUp() {
    $("#banner").slideUp(400);
    $(".btn-ctrl > span").addClass("close");
}

$(document).ready(function() {
    $(".btn-ctrl").click(function() {
        console.log('click')
        if ($(".btn-ctrl > span").hasClass("close")) {
            popDown();
        } else {
            popUp();
        }
    })
})
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