473,549 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.check ed = true;

<script language="javas cript">
function checker() {
var checkbox1 = document.getEle mentById('sch-1');
checkbox1.check ed = true;
var checkbox5 = document.getEle mentById('sch-5');
checkbox5.check ed = true;
var checkbox10 = document.getEle mentById('sch-10');
checkbox10.chec ked = 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"><inpu t name="sch" id="sch-15" type="checkbox"
value="sadfsadf " style="width:7p x;" /></td>
<td width="170"sadf sadf</td>
<td width="7"><inpu t name="sch" id="sch-14" type="checkbox"
value="sadfsadf " style="width:7p x;" /></td>

<td width="170"sadf sadf</td>
<tr>
<td width="7"><inpu t name="sch" id="sch-13" type="checkbox"
value="sadfsadf " style="width:7p x;" /></td>
<td width="170"sadf sadf</td>

<td width="7"><inpu t name="sch" id="sch-12" type="checkbox"
value="te" style="width:7p x;" /></td>
<td width="170"te</td>

</tr>
<tr>
<td width="7"><inpu t name="sch" id="sch-11" type="checkbox"
value="test2" style="width:7p x;" /></td>
<td width="170"test 2</td>

<td width="7"><inpu t name="sch" id="sch-10" type="checkbox"
value="Test Scholar" style="width:7p x;" /></td>
<td width="170"Test Scholar</td>
</tr>
<tr>

<td width="7"><inpu t name="sch" id="sch-5" type="checkbox"
value="Freeman-Asia Scholarship " style="width:7p x;" /></td>
<td width="170"Free man-Asia Scholarship </td>

<td width="7"><inpu t name="sch" id="sch-1" type="checkbox"
value="GO Scholarship" style="width:7p x;" /></td>
<td width="170"GO Scholarship</td>

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

<script language="javas cript">
function checker() {
var checkbox1 = document.getEle mentById('sch-1');
checkbox1.check ed = true;
var checkbox5 = document.getEle mentById('sch-5');
checkbox5.check ed = true;
var checkbox10 = document.getEle mentById('sch-10');
checkbox10.chec ked = true;
}
checker();
</script>
[...39 lines snipped...]
<td width="7"><inpu t name="sch" id="sch-1" type="checkbox"
value="GO Scholarship" style="width:7p x;" /></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.getEle mentById(). 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
4012
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 single form rather than 12 separate forms. If the checkbox contained in each day within each month has a unique name such as 1August2003,...
2
2423
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> <body> <form name="myform" action=test.php method=post>
2
3220
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 is a text box, with the search button next to it, and at the bottom there are four check boxes. When the form loads one of the two radio buttons are...
1
4219
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 the example to work. I created the following two classes, provided with the example: *-*-**-*-*-*-*-*-*-*-*-*-**-*-*-*-*-CheckBoxColumn...
10
5188
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. I set the AutoPostBack property of the CheckBox in the Header to True & am invoking a sub named 'CheckAllRows' on the CheckedChanged event of this...
9
1789
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; var l=document.forms.selected_files.length; alert(l);
4
14216
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 this "<input type='checkbox' name='checkbox' value='1'><input type='checkbox' name='checkbox' value='2'>" and the check all boxes function worked but...
1
1553
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 counties. The validation works fine. However, when the form is posted to my next page (myresults.asp), I need the values of the options to output the...
13
3057
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 checkboxes (code pasted below). But if name of the checkbox array is 'chkbx_ary' then it's failing. I want the name to be 'chkbx_ary' because I want to...
1
2400
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. I want to display these results in check boxes. This is my code. But it doesn't work <tr><td><input value="1" type="checkbox" id="1" name="check"...
0
7461
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7730
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7491
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3509
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3491
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1068
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.