views:

34665

answers:

5

I need to check the checked property of a checkbox and perform the 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 gives false as by default:

if ($('#isAgeSelected').attr('checked')) {
    $("#txtAge").show();
}
else
{
    $("#txtAge").hide();
}

Did I miss anything here?

+30  A: 

Try this:

if ($('#isAgeSelected').is(':checked')) { $("#txtAge").show(); } else { $("#txtAge").hide(); }

You can shorten this using ternary, some might say it's a bit less readable, but that's how I would do it:

$('#isAgeSelected').is(':checked') ? $("#txtAge").show() : $("#txtAge").hide();

EDIT (14 months later): There's a much prettier way to do this, using toggle:

$('#isAgeSelected').click(function() {
    $("#txtAge").toggle(this.checked);
});

<input type="checkbox" id="isAgeSelected"/>
<div id="txtAge" style="display:none">Age is something</div>​

Demo: http://jsfiddle.net/5udtC/

karim79
i have tried the above condition too, but it returns false only
Prasad
Something is amiss. In fact, the code you provided in your question should work. Are you referencing the correct element?
karim79
This is the checkbox i am using: <%= Html.CheckBox("isAgeSelected", new { id = "isAgeSelected", onclick = "showAge();" })%>Also i have checked with alert condition:alert($('#isAgeSelected').is(':checked'));but it popups false only
Prasad
I would try xenon's suggestion, also you might want to test this with a non server-generated checkbox in case it's not being rendered properly. Furthermore, you might want to consider getting rid of the onclick="showAge()" and replace that with a jQuery binding on the click event $('#isAgeSelected').click(function() {...
karim79
You may want to stick the line "debugger;" in your code and then step through it. This will allow you to debug it in your browser to see what happens. IE will launch VS, in FireFox it will launch Firebug if installed.Easier if you temporarily make the code more verbose, such as var test = $('#isAgeSelected').is(':checked');If test...You could even do var = $('#isAgeSelected') to see if you get the right element and what it's properties are.
Frans
@karim79 I know this is old, but his original selector is missing a single quote after the id $('#isAgeSelected).attr which should be $('#isAgeSelected').attr
Mark Schultheiss
$("#txtAge").toggle(this.checked);Does exactly the same as $("#txtAge").toggle(). So, if for some reason the state is checked and the next div is hidden (you clicked back button in your browser) - it won't work as expected.
negative
A: 

I believe you could do this:

if ($('#isAgeSelected :checked').size() > 0)
{
    $("#txtAge").show(); 
} else { 
    $("#txtAge").hide();
}
xenon
alert($('input[name=isAgeSelected]').attr('checked'));The above code shows true/false based on the check. Any problem with the previous tries
Prasad
Prasad, are you referencing your checkbox by name or ID? I'm getting confused now...
karim79
i have referenced it by name
Prasad
Can you not give it an ID? Things would be a lot easier, In your example you have address it by it's ID
xenon
+6  A: 

This worked for me.

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

where isAgeSelected is the id of the control.

Prasad
Are you not missing a #?
teedyay
A: 

The code is proper, you should check for mistake in other part of your code.

Yosef
A: 

I am using this and this is working absolutely fine:

if($("#checkkBoxId").attr("checked")==true)
{
    alert("Checked");
}
else
{
    alert("Unchecked");
}

Note: If the checkbox is checked it will return true otherwise undefined, so better check for the "TRUE" value.

Pradeep