By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,051 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

How to use DOM to check a checkbox

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.