muqeem khan muqeem khan - 1 month ago 8
jQuery Question

Conditional function based on either a checkbox is checked or not

$('#fastbill').click(function(){
if ($('#changevaluebill').attr('checked','true')){
$('#changevaluebill').attr("value","Recharge Now");
}
else {
$('#changevaluebill').attr("value","Proceed");
}
});


The checkbox condition is not working.

Answer

There are many ways to check if either a checkbox is checked

You can use is(':checked')

Try:

$('#fastbill').click(function(){
  if($('#changevaluebill').is(':checked')){
     $('#changevaluebill').val("Recharge Now");
  } else {
     $('#changevaluebill').val("Proceed");
  }
});

Other ways could be:

$('#changevaluebill').prop('checked') // Boolean
$('#changevaluebill:checked').length // Integer >0
$('#changevaluebill:checked').size() // .size() can be used instead of .length
$('#changevaluebill').get(0).checked // Boolean true
$('#changevaluebill')[0].checked // Boolean true (same as above)

Reference

Comments