469,360 Members | 1,807 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

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 21129
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by arenaTR | last post: by
1 post views Thread by Kate | last post: by
1 post views Thread by Mark | last post: by
1 post views Thread by vinod | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.