473,225 Members | 1,180 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,225 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 21383
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: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.