- This topic is empty.
-
AuthorPosts
-
May 23, 2009 at 10:16 am #8999
David Hoang
KeymasterI need to check the
checkedproperty of a checkbox and perform an action based on the checked property using jQuery.For example, if the
agecheckbox is checked, then I need to show a textbox to enterage, else hide the textbox.But the following code returns
falseby 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
checkedproperty?December 9, 2010 at 9:44 am #9023David Hoang
KeymasterI was having the same problem and none of the posted solutions seemed to work and then I found out that it’s because ASP.NET renders the CheckBox control as a SPAN with INPUT inside, so the CheckBox ID is actually an ID of a SPAN, not an INPUT, so you should use:
$('#isAgeSelected input')rather than
$('#isAgeSelected')and then all methods listed above should work.
May 11, 2011 at 4:36 am #9024David Hoang
KeymasterHere’s an example that includes initialising the show/hide to match the state of the checkbox when the page loads; taking account of the fact that firefox remembers the state of checkboxes when you refresh the page, but won’t remember the state of the shown/hidden elements.
$(function() { // initialise visibility when page is loaded $('tr.invoiceItemRow').toggle($('#showInvoiceItems').attr('checked')); // attach click handler to checkbox $('#showInvoiceItems').click(function(){ $('tr.invoiceItemRow').toggle(this.checked);}) });(with help from other answers on this question)
January 18, 2013 at 8:39 am #9005David Hoang
Keymasterif( undefined == $('#isAgeSelected').attr('checked') ) { $("#txtAge").hide(); } else { $("#txtAge").show(); }January 21, 2013 at 4:53 am #9013David Hoang
Keymasterif( undefined == $('#isAgeSelected').attr('checked') ) { $("#txtAge").hide(); } else { $("#txtAge").show(); }January 23, 2013 at 11:40 am #9020David Hoang
KeymasterUse:
$(this).toggle($("input:checkbox", $(this))[0].checked);When you are selecting out of context, remember you need the [0] to access the checkbox.
February 25, 2013 at 9:26 am #9021David Hoang
KeymasterThis was my workaround:
$('#vcGoButton').click(function () { var buttonStatus = $('#vcChangeLocation').prop('checked'); console.log("Status is " + buttonStatus); if (buttonStatus) { var address = $('#vcNewLocation').val(); var cabNumber = $('#vcVehicleNumber').val(); $.get('postCabLocation.php', {address: address, cabNumber: cabNumber}, function(data) { console.log("Changed vehicle " + cabNumber + " location to " + address ); }); } else { console.log("VC go button clicked, but no location action"); } });July 15, 2013 at 10:50 am #9026David Hoang
KeymasterThis is the minimal amount of code I think I needed to do something like this effectively. I found this method to be useful; it returns an array of the check boxes that are checked and then you can use their value (this solution uses jQuery):
// This is how you get them var output = ""; var checkedBoxes = $("DivCheckBoxesAreIn").children("input:checked"); if(checkedBoxes.length <= 0) { alert('Please select check boxes'); return false; }; // And this is how you use them: checkedBoxes.each(function() { output += this.value + ", "; };Printing “output” will give you a comma-separated list of your values.
October 10, 2013 at 1:23 am #9028David Hoang
KeymasterThe
checkedattribute of aninput type="checkbox"is mapped with thedefaultCheckedproperty, not with thecheckedproperty.So when doing something in a page when a checkbox is checked on uncheked, use the
prop()method instead. It fetches the property value and changes as the state of the checkbox changes.Using
attr() orgetAttribute(in pure JavaScript) in these cases are not the proper way of doing things.if
elemis the concerned checkbox then do something like this to fetch the value:elem.checkedor
$(elem).prop('checked')April 22, 2014 at 9:31 am #9022David Hoang
KeymasterSetter:
$("#chkmyElement")[0].checked = true;Getter:
if($("#chkmyElement")[0].checked) { alert("enabled"); } else { alert("disabled"); }May 14, 2014 at 12:42 pm #9029David Hoang
KeymasterAutomated
$(document).ready(function() { $('#isAgeSelected').change(function() { alert( 'value =' + $('#chkSelect').attr('checked') ); }); });HTML
<b> <input type="isAgeSelected" id="chkSelect" /> Age Check </b> <br/><br/> <input type="button" id="btnCheck" value="check" />jQuery
$(document).ready(function() { $('#btnCheck').click(function() { var isChecked = $('#isAgeSelected').attr('checked'); if (isChecked == 'checked') alert('check-box is checked'); else alert('check-box is not checked'); }) });Ajax
function check() { if (isAgeSelected()) alert('check-box is checked'); else alert('check-box is not checked'); } function isAgeSelected() { return ($get("isAgeSelected").checked == true); }August 30, 2014 at 5:36 am #9017David Hoang
KeymasterI would actually prefere the
changeevent.$('#isAgeSelected').change(function() { $("#txtAge").toggle(this.checked); });November 7, 2014 at 11:37 am #9002David Hoang
KeymasterIn case if you need to use CSS class as jQuery selector you can do following:
$(document).ready(function () { $('.myOptionCheckbox').change(function () { if ($(this).prop('checked') == true) { console.log("checked"); } else { console.log("unchecked"); } }); });It works fine for
checkboxesandradioboxesas well.January 6, 2015 at 7:34 am #9000David Hoang
Keymaster$("#isAgeSelected").prop('checked', true);March 30, 2015 at 4:45 am #9018David Hoang
KeymasterA selector returns multiple objects, and it must take the first item in the array:
// Collection var chckremember = $("#chckremember"); // Result boolen var ischecked=chckremember[0].checked; -
AuthorPosts
- You must be logged in to reply to this topic.