Checked property

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

Grrrr, the simplest things in JS never work for me...This pointless script will make it impossible to check the box. Works fine in Firefox but in Opera and IE, it just operates like a normal checkbox.

<script type="text/javascript">
function runMe()
{
status = document.getElementById("check-box");
status.checked = false
}
</script>

<input type="checkbox" id="check-box" onclick="runMe()"/>

(live code)

What am I doing wrong?

(I'm not looking to make an impossible to checkbox Wink, I've just broken it down to a simple form to see what my problem is)

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

Try this:

&lt;script type="text/javascript"&gt;
function runMe()
{
status = document.getElementById("check-box").checked = false;
}
&lt;/script&gt;

<input type="checkbox" id="check-box" onclick="runMe()"/>

When you run status = document.getElementById("check-box");, Firefox must assign a pointer or something whereas other browsers do not. In other words, when you assign the checkbox's status to the variable 'status', that variable, 'status' is not related to the checkbox, thus changing the variable does not change the checkbox, unless the variable is a pointer of sorts. That's what I'm guessing Firefox is treating it as.

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

Thanks pr0gr4mm3r! Works perfectly.

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

You're welcome. Cool

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.