473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 4606
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: John Banta | last post by:
Hi, I have created a 12 month calendar where each day has a check box whereby the user can indicate if that day is available or not available for a certain event. The calendar is 'drawn' in a...
2
by: Edward | last post by:
The following html / javascript code produces a simple form with check boxes. There is also a checkbox that 'checks all' form checkboxes hotmail style: <html> <head> <title></title> </head>...
2
by: jimi_xyz | last post by:
Sorry if this isn't the correct group, i don't think there is a group for straight HTML. I am trying to create a type of search engine. There are two radio buttons at the top, in the middle there...
1
by: sianan | last post by:
I tried to use the following example, to add a checkbox column to a DataGrid in an ASP.NET application: http://www.codeproject.com/aspnet/datagridcheckbox.asp For some reason, I simply CAN'T get...
10
by: rn5a | last post by:
All the rows in a DataGrid, including the Header, are accompanied with a CheckBox. I want that when the CheckBox in the Header is checked, then all the CheckBoxes should automatically get checked....
9
by: morellik | last post by:
Dear all, I have a program that creates dinamically a web page. In the page I have the following function to check how many checkbox are checked. function tarInfo(info) { var i=0; var c=0;...
4
by: it2051229 | last post by:
Well i'm having a problem with the compatibility of javascript and PHP multiple delete check box.. i used a javascript for the "CHECK ALL BOXES" just like yahoo mail.. so my input is something like...
1
by: rjoseph | last post by:
HI Guys I have a simple check box form (using GET as the method) with a list of 10 different UK counties. I have validated the form so that users have to select no more and no less than 5 UK...
13
by: PhpCool | last post by:
Hi, since sometime I'm stuck in a problem where I want to check or uncheck all the checkboxes. If I'm choosing name for the checkbox array as 'chkbx_ary' then I'm able to check/uncheck all the...
1
by: ghjk | last post by:
my php page has 7 check boxes. I stored checked values to database and retrive as binary values. This is the result array Array ( => 0 => 1 => 0 => 1 => 0 => 0 => 1 ) 1 means checked....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.