Skip to main content
added code formatting
Source Link
Anonymous
  • 796
  • 4
  • 15
  • 39

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 ageage checkbox is checked, then I need to show a textbox to enter ageage, else hide the textbox.

But the following code returns false by default:

if ($('#isAgeSelected').attr('checked')) {
  $("#txtAge").show();
} else {
  $("#txtAge").hide();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="isAgeSelected"/>
<div id="txtAge" style="display:none">
  Age is selected
</div>

How do I successfully query the checked property?

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();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="isAgeSelected"/>
<div id="txtAge" style="display:none">
  Age is selected
</div>

How do I successfully query the checked property?

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();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="isAgeSelected"/>
<div id="txtAge" style="display:none">
  Age is selected
</div>

How do I successfully query the checked property?

Reformatted the code
Source Link
Ritu
  • 712
  • 6
  • 14

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();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 
<input type="checkbox" id="isAgeSelected"/>
 
<div id="txtAge" style="display:none">
  Age is selected
</div>

How do I successfully query the checked property?

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();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 
<input type="checkbox" id="isAgeSelected"/>
 
<div id="txtAge" style="display:none">
Age is selected
</div>

How do I successfully query the checked property?

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();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="isAgeSelected"/>
<div id="txtAge" style="display:none">
  Age is selected
</div>

How do I successfully query the checked property?

Rollback to Revision 20
Source Link
ita
  • 802
  • 6
  • 9

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:

$(document).on("click","#isAgeSelected",function(){
        if ($(this'#isAgeSelected').propattr("checked"'checked') == true) 
{
            $("#txtAge").show();
        }
        else if($(this).prop("checked") == false) 
{
            $("#txtAge").hide();
        }
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

 <input type="checkbox" id="isAgeSelected"/>

<div id="txtAge" style="display:none">
<input type="text" name="age" placeholder="Please enterAge age"is />selected
</div>

How do I successfully query the checked property?

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:

$(document).on("click","#isAgeSelected",function(){
        if($(this).prop("checked") == true){
            $("#txtAge").show();
        }
        else if($(this).prop("checked") == false){
            $("#txtAge").hide();
        }
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

 <input type="checkbox" id="isAgeSelected"/>

<div id="txtAge" style="display:none">
<input type="text" name="age" placeholder="Please enter age" />
</div>

How do I successfully query the checked property?

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();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="checkbox" id="isAgeSelected"/>

<div id="txtAge" style="display:none">
Age is selected
</div>

How do I successfully query the checked property?

Update perfect answer
Source Link
Sarvesh Patel
  • 133
  • 1
  • 3
  • 12
Loading
Grammar - formatted properly as a question
Link
dagelf
  • 1.6k
  • 1
  • 15
  • 28
Loading
edited tags
Link
Kamil Kiełczewski
  • 90.2k
  • 32
  • 383
  • 361
Loading
edited tags
Link
mhrabiee
  • 815
  • 10
  • 23
Loading
added 383 characters in body
Source Link
Nishal K.R
  • 1.1k
  • 11
  • 21
Loading
Syntax error
Source Link
Nebulosar
  • 1.8k
  • 3
  • 22
  • 46
Loading
edited tags
Link
EAzevedo
  • 781
  • 2
  • 18
  • 46
Loading
Corrected formatting
Source Link
Loading
Improved title, added tags
Link
Steve Chambers
  • 38.7k
  • 28
  • 169
  • 217
Loading
Copy edited.
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 109
  • 132
Loading
Made title into a question in a consistent format with other top questions
Link
Steve Chambers
  • 38.7k
  • 28
  • 169
  • 217
Loading
correct typo in question title
Link
OutstandingBill
  • 2.8k
  • 1
  • 29
  • 38
Loading
edited title
Link
TylerH
  • 21.1k
  • 72
  • 78
  • 105
Loading
deleted 178 characters in body
Source Link
Michał Perłakowski
  • 91.3k
  • 29
  • 163
  • 184
Loading
why don't the good answers float to the top?
Source Link
coolaj86
  • 75.9k
  • 21
  • 112
  • 131
Loading
removed tag from title
Link
Umur Kontacı
  • 35.5k
  • 8
  • 74
  • 96
Loading
Post Made Community Wiki by Udit Bhardwaj
Question Protected by Mr. Alien
edited tags
Link
Bhanu Krishnan
  • 3.7k
  • 1
  • 21
  • 40
Loading
added 1 characters in body
Source Link
Prasad
  • 59.4k
  • 65
  • 153
  • 201
Loading
Code formatted as such. Added link to jQuery [Wikipedia]. Minor edit: grammar/spelling/case/punctation/etc.
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 109
  • 132
Loading