I have the following trivial page:
<html>
<head>
<script type="text/javascript">
function check(){
var cb = document.getElementById('aaa');
if(!cb.checked()){
alert("test!!!");
}
}
</script>
</head>
<body>
<input id="aaa" type="checkbox" name="modify" onclick="check()">Modify</input>
</body>
</html>
If I press the check box I see the following error in the javascript console:
Uncaught TypeError: Property 'checked' of object #<HTMLInputElement> is not a function
check
onclick
I also tried with documents.getElementsByName('modify')
and the same problem.
What am I doing wrong here?
I have the following trivial page:
<html>
<head>
<script type="text/javascript">
function check(){
var cb = document.getElementById('aaa');
if(!cb.checked()){
alert("test!!!");
}
}
</script>
</head>
<body>
<input id="aaa" type="checkbox" name="modify" onclick="check()">Modify</input>
</body>
</html>
If I press the check box I see the following error in the javascript console:
Uncaught TypeError: Property 'checked' of object #<HTMLInputElement> is not a function
check
onclick
I also tried with documents.getElementsByName('modify')
and the same problem.
What am I doing wrong here?
3 Answers
Reset to default 5checked
isn't a function, as the error message states.
Use
if(!cb.checked){
(without the parenthesis)
Note that you don't have to look for the element using getElementById
, you can pass the element :
<input id="aaa" type="checkbox" name="modify" onclick="check(this)">Modify
function check(cb){
if(!cb.checked()){
alert("test!!!");
}
}
Note also that the input
element should not be closed. What you probably need here is the label element
, which lets you click on the text to (un)check the box :
<input id="aaa" type="checkbox" name="modify" onclick="check(this)">
<label for=aaa>Modify</label>
remove the brackets from cb.checked() - it is not a function. This code should work:
<html>
<head>
<script type="text/javascript">
function check(){
var cb = document.getElementById('aaa');
if(!cb.checked){
alert("test!!!");
}
}
</script>
</head>
<body>
<input id="aaa" type="checkbox" name="modify" onclick="check()">Modify</input>
</body>
</html>
You can try with jquery also and you wrongly added end tag for input type
<script type="text/javascript">
$(document).ready(function(){
$('#aaa').click (function ()
{
if ($(this).is(':checked'))
{
// Do stuff
alert("test!!!");
}
});
});
</script>
<input id="aaa" type="checkbox" name="modify" />