0

set check working for two time only.
->once it checked -> then unchecked
but i want this something like toggle. please fix this code.

$('#a').click(function() {
  if ($("#radioinstant").is(':checked')) {
$("#radioinstant").removeAttr('checked', 'checked'); //This line
}else{
$("#radioinstant").attr('checked', 'unchecked');
}

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
    With JQuery Function: <input type="checkbox" id="radioinstant"/>
    <a id="a" href="#">set check</a>
</div>

thanks in advance

Gautam Jha
  • 1,445
  • 1
  • 15
  • 24

1 Answers1

1

You can simplify the logic by providing a function to the prop() class which inverts the current setting. Try this:

$('#a').click(function() {
  $("#radioinstant").prop('checked', function(i, checked) {
    return !checked;
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
  With JQuery Function:
  <input type="checkbox" id="radioinstant" />
  <a id="a" href="#">set check</a>
</div>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339