jQuery addClass example

Change class name on click in jQuery

by 20yco

HTML

<input id="rad1" value="1" type="radio" name="rad"/><label for="rad1">Radio 1</label>
<input id="rad2" value="2" type="radio" name="rad"/><label for="rad2">Radio 2</label>
<input id="rad3" value="3" type="radio" name="rad"/><label for="rad3">Radio 3</label>

JavaScript

var curr = '';
var prev = '';

$('input').on('click', function(){

  if( curr == $(this).attr('id') ){

    if( prev != '' ){

			$("#"+prev).prop('checked',true);

			prev = '';
      
    }
    
  	return;
  }
  
  prev = curr;
  
  curr = $(this).attr('id');
  
});