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

121
Vistas
jQuery listen for radio state change, for each radio

$('input').on('change', function () {
    console.log(this.outerHTML);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="one">one</label><input type="radio" name="tar" id="one" checked>
<label for="two">two</label><input type="radio" name="tar" id="two">
<label for="three">three</label><input type="radio" name="tar" id="three">
<label for="four">four</label><input type="radio" name="tar" id="four">

The change event seems to only be fired once when the selected radio button is changed, and only for the radio button which has just been selected.

So in the above demo, whichever button you click on, that is the button that you get shown the HTML of.

However, I would like the listener to be run against the previously selected radio button element as well as the new one.

How can I do that?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Consider please following.

To store a selection in variable, i.e. at the beginning and after each update.

Please see the snippet, where variable "pastEl" is used for it.

var rbName = 'tar';
var newEl = null, pastEl = null;

function print(el) {
  
  console.log(el.outerHTML);
  
}

(function() {
  
  pastEl = $(`input[name="${rbName}"]:checked`)[0];

  $('input').on('change', function () {
      newEl = $(this)[0];
      print(pastEl);
      print(newEl);
      pastEl = newEl;
  });
  
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<label for="one">one</label><input type="radio" name="tar" id="one" checked>
<label for="two">two</label><input type="radio" name="tar" id="two">
<label for="three">three</label><input type="radio" name="tar" id="three">
<label for="four">four</label><input type="radio" name="tar" id="four">

about 4 years ago · Juan Pablo Isaza Denunciar

0

I'm not sure if this is what you are looking for but I hope it works for you. Try this JS code

var num1
var num2
$('input').on('click', function () {    
    num2=num1
    num1 = this.outerHTML;
    
  console.log(num2)
  console.log(num1)
  
});

It could also work with $('input').on('change', function ()

Let me know if it helped you

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