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

198
Visualizações
Simple jQuery if else statement

I have a mini script, this script finds within a specific class the link. I basically need a simple if-else-statement. Something like if a is existing, then run the script if not, then do nothing.

Can someone help me with that?

jQuery(document).ready(function() {
  jQuery("body").on("click", ".click-child", function() {
      var href = jQuery(this).find("a").attr('href');
      window.location = href;
  });
});

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

0

You can use the :has() selector to only hook the event handler to the .card elements which contain an a. Then you don't need the if statement at all. Try this:

jQuery($ => {
  $("body").on("click", ".click-child:has(a)", function() {
    var href = $(this).find("a").attr('href');
    window.location = href;
  });
});
about 4 years ago · Juan Pablo Isaza Relatório

0

Based on your question, this is what I imagine you're asking for. If you could put your HTML somewhere it would be more clear what you're looking for.

jQuery has a .find() method which searches the parent selector for a child selector, if it finds the child selector, you can use it as a normal jQuery selector. Otherwise, it'll do nothing.

If this isn't what you're looking for, please add a little more detail, and I'll see if I can help your out more.

<section class="cards">
    <div class="card">
        <img />
        <h3>Opens link</h3>
        <a href="https://google.com">Link</a>
    </div>
    <div class="card">
        <img />
        <h3>Nothing should happen</h3>
    </div>
</section>

<script>
    jQuery(window).ready( function() {
        jQuery('.card').click(function() {
            const link = jQuery(this).find('a').attr("href")
            
            if (link) {
                window.location = link;
            }         
        });
    });
</script>
about 4 years ago · Juan Pablo Isaza Relatório

0

You don't need an if/else. You can do an .each() over a list (which might either contain one element, or be empty):

jQuery(function() {
  jQuery("body").on("click", ".click-child", function () {
    jQuery(this).find("a").first().each(function () {
      window.location = this.href;
    });
  });
});
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