Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!