Mimi
Mimi

Reputation: 389

Can't get HTML checkbox value in JavaScript

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?

Upvotes: 1

Views: 1949

Answers (4)

Zaki
Zaki

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

NikolaiDante
NikolaiDante

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

Nithya Chandrashekaran
Nithya Chandrashekaran

Reputation: 116

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

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

Upvotes: 1

Hemal
Hemal

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

Related Questions