I need to check the checked
property of a checkbox and perform an action based on the checked property using jQuery.
For example, if the age checkbox is checked, then I need to show a textbox to enter age, else hide the textbox.
But the following code returns false
by default:
if ($('#isAgeSelected').attr('checked'))
{
$("#txtAge").show();
}
else
{
$("#txtAge").hide();
}
How do I successfully query the checked
property?
If you are using an updated version of jquery, you must go for
.prop
method to resolve your issue:$('#isAgeSelected').prop('checked')
will returntrue
if checked andfalse
if unchecked. I confirmed it and I came across this issue earlier.$('#isAgeSelected').attr('checked')
and$('#isAgeSelected').is('checked')
is returningundefined
which is not a worthy answer for the situation. So do as given below.Hope it helps :)- Thanks.
This example is for button.
Try the following:
Use:
Use:
This can help if you want that the required action has to be done only when you check the box not at the time you remove the check.
I am using this: