471,896 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,896 software developers and data experts.

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 4547
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 YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.