Reputation: 93
I have a CheckBox control within a asp:Panel like below:
<asp:Panel ID="chk" runat="server" Visible="false" CssClass="checkbox checkbox-primary checkbox-single">
<asp:CheckBox runat="server" ID="valCheck" />
</asp:Panel>
I need to be able to check to see if this CheckBox is being checked or not then I would send 0 or 1 to a function that will do some more stuff and when comes back based on the value, if it's more than 0 it will display a checked check box. I need to use this value, because I also have some other types of inputs as well which I set their value based on the value coming back from another call.
var checking = false;
function saveCheckBoxData(obj, id, inputType) {
if (checking) return;
checking = true;
var date = $("#MainContent_date").val();
var column = $(obj).attr("column");
var dataFieldId = $(obj).attr("dataFieldId ");
var shift = $(obj).attr("shift");
var val = 0;
//if ($("#valCheck").is(':checked')) {
// val = 1;
//}
//if ($('#valCheck').prop('checked')) {
// val = 1;
//}
//if ($('#valCheck').attr("checked")) {
// val = 1;
//}
SaveInput(dataFieldId , shift, inputType, date, column, id, val);
}
When I call SaveInput function, I need to either pass 0 (if checkbox is not checked) or 1 (when it's checked). The lines I've commented out did not work!
Any feedback would be greatly appreciated!
Upvotes: 3
Views: 5186
Reputation: 1697
The id is not exactly valCheck
when you use runat="server"
the element id changes(inspect the element to see the new id), then you need to use the jquery selector like:
$("select[id*='valCheck']")
or
$("select[id$='valCheck']")
or you can simply add the attr ClientIdMode="Static"
in your asp element.
<asp:CheckBox runat="server" ID="valCheck" ClientIdMode="Static" />
If you want to understand why it happens, take a look at that article.
Upvotes: 3
Reputation: 1323
In plain javascript
var isChecked= document.getElementById("myCheck").checked;
isChecked
will be true
if checked and false
if not checked
Upvotes: 2