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


i need check checked property of checkbox , perform action based on checked property using jquery.

for example, if age checkbox checked, need show textbox enter age, else hide textbox.

but following code returns false default:

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

how query checked property?

how query checked property?

the checked property of checkbox dom element give checked state of element.

given existing code, therefore this:

if(document.getelementbyid('isageselected').checked) {     $("#txtage").show(); } else {     $("#txtage").hide(); } 

however, there's prettier way this, using toggle:

$('#isageselected').click(function() {      $("#txtage").toggle(this.checked);  });
<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 something</div>


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -