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

125
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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