Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

197
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda