473,387 Members | 1,574 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,387 software developers and data experts.

checking checkboxes onload

Hi

This is not worlking for me, I want all the checkboxes to be checked by
default, what is wrong?
<body onload="document.getElementsByTagName('input').che cked=true">

cheers Dave

Apr 18 '06 #1
8 21395
li********@yahoo.com.au schrieb:
Hi

This is not worlking for me, I want all the checkboxes to be checked by
default, what is wrong?
<body onload="document.getElementsByTagName('input').che cked=true">

cheers Dave


You should do that by generating a 'checked' attribute inside <input/>
elements.

or use something like....

[snip]

function heyholetsgo() {
var ins = document.getElementByTagName('input');
for (var i=0; i<ins.length; i++) {
if (ins[i].getAttribute('type') == 'checkbox') ins[i].value =
"checked";
}
}

[/snip]

(or slightly similar), code is more pseudo...
cheers
rob
Apr 18 '06 #2
li********@yahoo.com.au said on 18/04/2006 12:05 PM AEST:
Hi

This is not worlking for me, I want all the checkboxes to be checked by
default, what is wrong?
Your script is flawed (see below).

The simple solution is to include an HTML checked attribute, no need for
client scripting at all. An added bonus is that if you have a reset
button, resetting the form will return the checkboxes to checked.

<body onload="document.getElementsByTagName('input').che cked=true">


getElementsByTagName returns an HTML collection object. It doesn't have
a checked property, giving it one and setting it to true will likely
have no useful impact on the document.

If you want to set all checkboxes to true using script, then loop
through the collection and set the checked property of the individual
items to true:

var allInputs = document.getElementsByTagName('input');
var input, i = allInputs.length;
while (i--){
input = allInputs[i];
if ('checkbox' == input.type){
input.checked = true;
}
}


--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
Apr 18 '06 #3

"RobG" <rg***@iinet.net.au> wrote in message
news:lJ*******************@news.optus.net.au...
li********@yahoo.com.au said on 18/04/2006 12:05 PM AEST:
Hi

Your script is flawed (see below).

The simple solution is to include an HTML checked attribute, no need for
client scripting at all. An added bonus is that if you have a reset
button, resetting the form will return the checkboxes to checked.

<body onload="document.getElementsByTagName('input').che cked=true">


getElementsByTagName returns an HTML collection object. It doesn't have
a checked property, giving it one and setting it to true will likely
have no useful impact on the document.

If you want to set all checkboxes to true using script, then loop
through the collection and set the checked property of the individual
items to true:

var allInputs = document.getElementsByTagName('input');
var input, i = allInputs.length;
while (i--){
input = allInputs[i];
if ('checkbox' == input.type){
input.checked = true;
}
}


How would you check certain ones using the URL? I've seen that:
a page is linked to, e.g.:
http://somedomain.com/index.html?c1=y&c7=y
and on page load checkboxes with id=c1 and id=c7 are checked.

Maggie Blue
Apr 18 '06 #4
Thanks for the replies

ending up using the following

function checkall() {
var ins = document.getElementsByTagName('input');
for (var i=0; i<ins.length; i++) {
if (ins[i].getAttribute('type') == 'checkbox') ins[i].checked =
true;
}
Another problem

function uncheck() {
var input = document.getElementById('top');
input.checked = false;
}
The Firefox Javascript console says
Error: input has no properties

What does that mean?

cheers Dave

Apr 18 '06 #5
Maggie Blue said on 19/04/2006 1:53 AM AEST:
[...]
How would you check certain ones using the URL? I've seen that:
a page is linked to, e.g.:
http://somedomain.com/index.html?c1=y&c7=y
and on page load checkboxes with id=c1 and id=c7 are checked.


Use window.location.search which returns the ? and everything after it
from the URL.

<URL:http://developer.mozilla.org/en/docs/DOM:window.location#Properties>
In the above case:

var searchString = window.location.search.substring(1);
// searchString is now c1=y&c7=y
Split it on the '&' to get an array of the parts, then for each part
split on the '=' to get the name/id and flag.

var searchString = window.location.search.substring(1);

if ( /=/.test(searchString) ){
var nameVals = searchString.split('&');
var nameVal, el;

for (var i=0, len=nameVals.length; i<len; ++i){
nameVal = nameVals[i].split('=');

if ( nameVal[1].toLowerCase() == 'y'
&& (el = document.forms['fName'].elements[nameVal[0]])){
el.checked = true;
}
}
}

--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
Apr 19 '06 #6
li********@yahoo.com.au wrote:
Thanks for the replies

ending up using the following

function checkall() {
var ins = document.getElementsByTagName('input');
for (var i=0; i<ins.length; i++) {
if (ins[i].getAttribute('type') == 'checkbox') ins[i].checked =
true;
}

Another problem

function uncheck() {
var input = document.getElementById('top');
input.checked = false;
}

The Firefox Javascript console says
Error: input has no properties

What does that mean?


That means that it's not finding an element with the ID 'top'

Check the ID's that you've assigned to the <input>s - and be sure
they're ID's, not name's.
Apr 19 '06 #7

li********@yahoo.com.au wrote:

function uncheck() {
var input = document.getElementById('top');
input.checked = false;
}
The Firefox Javascript console says
Error: input has no properties

What does that mean?


I believe it means that 'input' is a reserved word. Other browsers may
allow it, but Firefox does not.

Stan Scott
New York City

Apr 23 '06 #8
st*****@gmail.com wrote:
li********@yahoo.com.au wrote:
function uncheck() {
var input = document.getElementById('top');
input.checked = false;
}
The Firefox Javascript console says
Error: input has no properties

What does that mean?
I believe it means that 'input' is a reserved word.


It does not mean that 'input' is a reserved word, and it is not a
reserved word. The complete list of reserved words and future reserved
words can be found in ECMA 262, 3rd Ed. Section 7.5.

The error means that input has no properties, that is, it is not an
object (of any kind). And since input has been assigned the result of a
call to getElementById, which returns either an Element reference (a
host object) or null, the conclusion would be that the method call
returned null (which is not an object and so has no properties). Thus
the probability is that there is no element in the DOM with an ID
attribute with the value 'top', either at all or at the time that the
method is called.
Other browsers
may allow it, but Firefox does not.


Unlike IE, Firefox takes a strict attitude towards what qualifies as an
ID attribute, excluding, for example, NAME attributes from that category
(not an unreasonable attitude).

Richard.
Apr 24 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: arenaTR | last post by:
I have a group of five checkboxes that I need to check in a script after a post-operaiton. I was using the code: $drv1 = $_POST but if the user left checkbox "driving_1" empty, i get a php...
1
by: Kate | last post by:
I have an PHP page that contains several checkboxes. I need to know if the user has check all of the checkboxes before they can proceed to the next page. How do I check there status? And can I...
4
by: feanor | last post by:
I need to select children checkboxes when selecting the parent one. This is my function: function SelectChildrens(checkbox_name){ form = document.forms; Sname = checkbox_name.split("-"); for...
13
by: aundro | last post by:
Hello, I've been looking on the web for a solution to this problem: I create a set of checkboxes, and 2 buttons: - one is labeled "All" - the other is labeled "None" Clicking "All" is...
8
by: Tim | last post by:
On my form I have 10 checkboxes named chkbox1,chkbox2,....chkbox10. I would have like to set it up as an array control like in VB6 where I could have chkbox(1),chkbox(2) but I think .net you have...
1
by: Mark | last post by:
Hello, I am using the new TreeView in ASP.Net 2.0. I am using its PopulateOnDemand feature. The problem is that the treeviews +/- images, and any checkboxes, have the tooltip set. If the node...
1
by: vinod | last post by:
Hi, I have a form wherein there are around 200-300 rows of homogenous data, each having around 30 input fileds of its own.So totally, there are more than 6000 input fields . Now, when the user...
10
by: Lostmind | last post by:
I am not a java expert, I am probably lower then a newbie. Here it goes, I am working with phpbb and java. My goal is to replace the checkboxes that are used by phpbb3 software. I am using this...
3
by: sakkat | last post by:
Hi, I should have a table with 2 columns in it, the first column will contain 10 checkboxes(the number is not fixed it changes dynamically) and a button(move) and the second column will be empty,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.