469,326 Members | 1,306 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,326 developers. It's quick & easy.

How to use DOM to check a checkbox

I have no idea why this code is not working, but believe that it
should be. I'm still a novice at javascript, and would really
appreciate any help.

The error I'm getting with firebug is:
checkbox1 is null
[Break on this error] checkbox1.checked = true;

<script language="javascript">
function checker() {
var checkbox1 = document.getElementById('sch-1');
checkbox1.checked = true;
var checkbox5 = document.getElementById('sch-5');
checkbox5.checked = true;
var checkbox10 = document.getElementById('sch-10');
checkbox10.checked = true;
}
checker();
</script>

</pre><!--<h1>8 Scholarships Found</h1>
<p><a href="add-scholarship.php" target="_parent">Add Scholarships</
a></p>-->
<table width="70%" border="0" cellspacing="0" cellpadding="0"
id="sch_array">

<td width="7"><input name="sch" id="sch-15" type="checkbox"
value="sadfsadf" style="width:7px;" /></td>
<td width="170"sadfsadf</td>
<td width="7"><input name="sch" id="sch-14" type="checkbox"
value="sadfsadf" style="width:7px;" /></td>

<td width="170"sadfsadf</td>
<tr>
<td width="7"><input name="sch" id="sch-13" type="checkbox"
value="sadfsadf" style="width:7px;" /></td>
<td width="170"sadfsadf</td>

<td width="7"><input name="sch" id="sch-12" type="checkbox"
value="te" style="width:7px;" /></td>
<td width="170"te</td>

</tr>
<tr>
<td width="7"><input name="sch" id="sch-11" type="checkbox"
value="test2" style="width:7px;" /></td>
<td width="170"test2</td>

<td width="7"><input name="sch" id="sch-10" type="checkbox"
value="Test Scholar" style="width:7px;" /></td>
<td width="170"Test Scholar</td>
</tr>
<tr>

<td width="7"><input name="sch" id="sch-5" type="checkbox"
value="Freeman-Asia Scholarship " style="width:7px;" /></td>
<td width="170"Freeman-Asia Scholarship </td>

<td width="7"><input name="sch" id="sch-1" type="checkbox"
value="GO Scholarship" style="width:7px;" /></td>
<td width="170"GO Scholarship</td>

</table>
Oct 20 '08 #1
2 4515
On 2008-10-20 20:52, ameshkin wrote:
The error I'm getting with firebug is:
checkbox1 is null
[Break on this error] checkbox1.checked = true;

<script language="javascript">
function checker() {
var checkbox1 = document.getElementById('sch-1');
checkbox1.checked = true;
var checkbox5 = document.getElementById('sch-5');
checkbox5.checked = true;
var checkbox10 = document.getElementById('sch-10');
checkbox10.checked = true;
}
checker();
</script>
[...39 lines snipped...]
<td width="7"><input name="sch" id="sch-1" type="checkbox"
value="GO Scholarship" style="width:7px;" /></td>
<FAQENTRY>

You cannot use the DOM to access elements that only appear in the
document *after* you try to read them with a DOM method like
document.getElementById(). You can either
a) put your script below the HTML where your element is defined, or
b) use the "load" event to trigger your script.

One way for (b) would be: <body onload="checker()">.
- Conrad
Oct 20 '08 #2
Thanks man, I had a feeling it was something silly. I keep forgetting
about that little rule. It's working great now.
Oct 20 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Edward | last post: by
2 posts views Thread by jimi_xyz | last post: by
9 posts views Thread by morellik | last post: by
1 post views Thread by rjoseph | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.