1

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' of undefined or null 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?

6
  • document.getElementById('myCheck') is that worked ? Commented Feb 10, 2016 at 10:45
  • It seems that error is thrown somewhere else, can you attach debugger in browser and check where this exception is thrown ? Commented Feb 10, 2016 at 10:45
  • @Amey Deshpande it doesn't fire the error but doesn't work too. Commented Feb 10, 2016 at 10:49
  • 2
    Original Question was for ASP.NET checkbox, approved edit is now HTML checkbox.What the hell. Commented Feb 10, 2016 at 10:50
  • @mimi: re: update - if you have a follow up question ask a new one :-) Commented Feb 10, 2016 at 11:10

4 Answers 4

1

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)

Sign up to request clarification or add additional context in comments.

2 Comments

# needed?I think no hash needed
Yes, I think I may have gone to jquery by accident.
1

Since checkbox is not an ASP control , you can do as

var subgrp = document.getElementById('myCheck').checked

Comments

1

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>

Comments

0

It should be

var Subgrp = <%=myCheck.Checked%>;

If checkbox is HTML

function IsSubGroupNeeded() {
    var Subgrp = document.getElementById('myCheck').checked;
    if (Subgrp == true) {
        loadgrdSubgroupItem();
    }
};

3 Comments

Wouldn't this be set on form rendering and never ever change on checkbox clicks?
OP told it is an ASP.NET checkbox, so <%=myCheck.Checked%>; will work perfectly.
I oppose this downvote. Can the person explain downvote?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.