Reputation: 389
I'm trying to get ASP.NET checkbox value which can be user input or retrieved from database. But each time it's showing this error message
0x800a138f - JavaScript runtime error: Unable to get property '
value
' ofundefined
ornull
reference
My HTML code:
<input type="checkbox" id="myCheck" data-member="IsSubGroupNecessary" data-label="" data-provider="checkbox" onchange="IsSubGroupNeeded();"/>
JavaScript Code:
function IsSubGroupNeeded() {
var Subgrp = document.getElementById('<%=myCheck.ClientID%>').checked;
if (Subgrp == true) {
loadgrdSubgroupItem();
}
};
Update: The error issue is solved. But I want to call loadgrdSubgroupItem function when checkbox value is true whether it is user input or retrieved from database. How to do it?
Upvotes: 1
Views: 1949
Reputation: 5600
You need to pass the name of the control only to getElementById
to find that element here is an example:
<html>
<head>
<script>
function IsSubGroupNeeded() {
var Subgrp = document.getElementById('myCheck').checked;
alert(Subgrp)
};
</script>
</head>
<body>
<input type="checkbox" id="myCheck" data-member="IsSubGroupNecessary" data-label="" data-provider="checkbox" onchange="IsSubGroupNeeded();"/>
</body>
</html>
Upvotes: 1
Reputation: 18639
Your rendered HTML has an id of myCheck
so you should be able to:
function IsSubGroupNeeded() {
var Subgrp = document.getElementById('myCheck').checked;
if (Subgrp == true) {
loadgrdSubgroupItem();
}
};
Without the need for the server side call.
(I assume that you have ClientIDMode="Static" on your asp.net markup)
Upvotes: 1
Reputation: 116
Since checkbox is not an ASP control , you can do as
var subgrp = document.getElementById('myCheck').checked
Upvotes: 1
Reputation: 3760
It should be
var Subgrp = <%=myCheck.Checked%>;
If checkbox is HTML
function IsSubGroupNeeded() {
var Subgrp = document.getElementById('myCheck').checked;
if (Subgrp == true) {
loadgrdSubgroupItem();
}
};
Upvotes: 0