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

201
Vistas
jQuery multiple events including an IF statement

Does anybody know how to combine these two events in jQuery?

The closest I could get was having 2 duplicate scripts that respond to 2 different events, which in my opinion is absolutely terrible. But it's the only way I could think of to do this for now. Both scripts do the exact same thing (or A LOT of things actually). I know jQuery has an option to add multiple event handlers to the same element, but how does this work if one of the events contains an if condition?

This runs when the user clicks on a radio button in a radio group:

$(document).ready(function() {

  // When radio button is clicked
  $("input[name='select']").click(function() {
    var select = $("input[name='select']:checked").val();
    $.ajax({
      type: "POST",
      url: "script.class.php",
      data: { select: select },
      cache: false,
      success: function(data) {
        // A lot of things will happen here...
      },
      error: function(xhr, status, error) {
        console.error(xhr);
      }
    });
  });     
});

This runs IF the radio group already has a radio button selected:

$(document).ready(function() {

  // If value is already present
  if ($("input[name='select']:checked").val()) {
    var select = $("input[name='select']:checked").val();
    $.ajax({
      type: "POST",
      url: "script.class.php",
      data: { select: select },
      cache: false,
      success: function(data) {
        // The same things as in the previous script will also happen here...
      },
      error: function(xhr, status, error) {
        console.error(xhr);
      }
    });
  }
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Put all the common code in a function and then call it

$(document).ready(function() {

  // When radio button is clicked
  $("input[name='select']").click(commonCode);
  if ($("input[name='select']:checked").val()) {
    commonCode();
  }
  function commonCode(){  
    var select = $("input[name='select']:checked").val();
    $.ajax({
      type: "POST",
      url: "script.class.php",
      data: { select: select },
      cache: false,
      success: function(data) {
        // A lot of things will happen here...
      },
      error: function(xhr, status, error) {
        console.error(xhr);
      }
    });
  }     
});
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