Tuesday 14 May 2019

javascript - How do I check whether a checkbox is checked in jQuery?



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?


Answer



This worked for me:



$get("isAgeSelected ").checked == true



Where isAgeSelected is the id of the control.



Also, @karim79's answer works fine. I am not sure what I missed at the time I tested it.



Note, this is answer uses Microsoft Ajax, not jQuery


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...