checking checkboxes onload

L

linuxnooby

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').checked=true">

cheers Dave
 
R

Robert Degen

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').checked=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.getAttribute('type') == 'checkbox') ins.value =
"checked";
}
}

[/snip]

(or slightly similar), code is more pseudo...


cheers
rob
 
R

RobG

(e-mail address removed) 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').checked=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;
if ('checkbox' == input.type){
input.checked = true;
}
}
 
M

Maggie Blue

RobG said:
(e-mail address removed) said on 18/04/2006 12:05 PM AEST:

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').checked=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;
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
 
L

linuxnooby

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.getAttribute('type') == 'checkbox') ins.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
 
R

RobG

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.split('=');

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

stannyc

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
 
R

Richard Cornford

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top