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

tic the boxes - I also accidentally posted this one in alt.html

Hi Gurus

I am sorry to ask so many questions, but unfortunately, you are just such an
awesome source of knowledge that I can not help myself but ask lots. One
day, I hope to be able to answer other people's questions.

Here we go, I have the following function (I made it myself ...!)

function tic(form,a,z){
for(f=a;z;f++){
if(form[f].type=="checkbox"){
form[f].checked=true;
}
}
}

It is supposed to tick a range of tickboxes (e.g. 1-10 or 11-30), I want to
use a and z to identify the range.

I use the following to call the function:

<LABEL FOR="0">
<INPUT TYPE="checkbox" VALUE="0" NAME="0" CLASS="ix"
ONCLICK="tic(this.form,0,1);">
New Zealand Wide</LABEL>

However, even with a range from 0 to 1, it will tick all the boxes in the
form. Secondly, does anyone know how to make the reverse function? Can I
add a fourth variable that indicates whether the current tickbox is true or
false and then use this in the function?

I hope it all makes sense

Thank you once more for all your help

- Nicolaas

Jul 23 '05 #1
12 1106
On Wed, 24 Nov 2004 10:45:31 +1300, WindAndWaves <ac****@ngaru.com> wrote:

[snip]
function tic(form,a,z){
for(f=a;z;f++){
The first problem you describe is caused by the condition (middle)
expression: it's not a comparison. As long as z is non-zero, the loop will
constantly execute. The only reason why this function would exit is
because eventually you attempt to index a nonexistent element and try an
access a property, which will cause an error.
if(form[f].type=="checkbox"){
form[f].checked=true;
}
}
}
This solves both problems:

function setRange(form, start, end, value) {
for(var i = start, e = form.elements; i <= end; ++i) {
if('checkbox' == e[i].type) {
e[i].checked = value;
}
}
}

[snip]
<LABEL FOR="0">
<INPUT TYPE="checkbox" VALUE="0" NAME="0" CLASS="ix"


The for attribute must match an id, not a name, and an id cannot start
with a number. Also, I wouldn't name a form control with a number. It
might interfere with indexing controls by ordinal.

[snip]

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2

"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:opshxuyb06x13kvk@atlantis...
On Wed, 24 Nov 2004 10:45:31 +1300, WindAndWaves <ac****@ngaru.com> wrote:

[snip]
function tic(form,a,z){
for(f=a;z;f++){


The first problem you describe is caused by the condition (middle)
expression: it's not a comparison. As long as z is non-zero, the loop will
constantly execute. The only reason why this function would exit is
because eventually you attempt to index a nonexistent element and try an
access a property, which will cause an error.
if(form[f].type=="checkbox"){
form[f].checked=true;
}
}
}


This solves both problems:

function setRange(form, start, end, value) {
for(var i = start, e = form.elements; i <= end; ++i) {
if('checkbox' == e[i].type) {
e[i].checked = value;
}
}
}

[snip]
<LABEL FOR="0">
<INPUT TYPE="checkbox" VALUE="0" NAME="0" CLASS="ix"


The for attribute must match an id, not a name, and an id cannot start
with a number. Also, I wouldn't name a form control with a number. It
might interfere with indexing controls by ordinal.

[snip]

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.


That definitely works! The only thing I was wondering is how I pass the
value of the tickbox that calls the function (i.e. if the user ticks the new
Zealand wide box then all the regions will be ticked and vice versa)

Thank you - you are a legend (as noted in a recent post).
- Nicolaas
Jul 23 '05 #3

"WindAndWaves" <ac****@ngaru.com> wrote in message
news:%k******************@news.xtra.co.nz...
That definitely works! The only thing I was wondering is how I pass the
value of the tickbox that calls the function (i.e. if the user ticks the new Zealand wide box then all the regions will be ticked and vice versa)

[ ... ]

I thought I solved it like this ....

function tic(form, start, end, value) {
for(var i = start, e = form.elements; i <= end; ++i) {
if('checkbox' == e[i].type) {
e[i].checked = !e[start].value;
}
}
}

but it does not seem to work, any hints???

TIA

-Nicolaas
Jul 23 '05 #4
WindAndWaves wrote:
[...]
That definitely works! The only thing I was wondering is how I pass the
value of the tickbox that calls the function (i.e. if the user ticks the new
Zealand wide box then all the regions will be ticked and vice versa)

Thank you - you are a legend (as noted in a recent post).


You can either pass this.value or pass this when calling the function
from the onclick event. If you just pass this, you can get its value
from inside the function as below:

<script type="text/javascript">

function doStuff(cb) {

// A bit of debug, remove when at will
alert('You clicked ' + cb.id
+ ' that has a value of '
+ cb.value);

if (cb.checked) {
// do stuff if cb has been checked
} else {
// do stuff if cb has been unchecked
}
}
</script>

...
<LABEL FOR="cb0">Here is the label for cb0
<INPUT TYPE="checkbox" VALUE="0" id="cb0" CLASS="ix"
onclick="doStuff(this);">
</label>
...

You don't need to use the for attribute of the label if the input is
nested as above, whether you do or not is up to you.

--
Rob
Jul 23 '05 #5

"RobG" <rg***@iinet.net.auau> wrote in message
news:sL*****************@news.optus.net.au...
WindAndWaves wrote:
[...]
That definitely works! The only thing I was wondering is how I pass the
value of the tickbox that calls the function (i.e. if the user ticks the new Zealand wide box then all the regions will be ticked and vice versa)

Thank you - you are a legend (as noted in a recent post).


You can either pass this.value or pass this when calling the function
from the onclick event. If you just pass this, you can get its value
from inside the function as below:

<script type="text/javascript">

function doStuff(cb) {

// A bit of debug, remove when at will
alert('You clicked ' + cb.id
+ ' that has a value of '
+ cb.value);

if (cb.checked) {
// do stuff if cb has been checked
} else {
// do stuff if cb has been unchecked
}
}
</script>

...
<LABEL FOR="cb0">Here is the label for cb0
<INPUT TYPE="checkbox" VALUE="0" id="cb0" CLASS="ix"
onclick="doStuff(this);">
</label>
...

You don't need to use the for attribute of the label if the input is
nested as above, whether you do or not is up to you.

--
Rob


I tried that, but it aint working (it must be something stupid)

function tic(form, start, end, el) {
var v = false
if (el.value) {v = true;} else {v = false;}
alert (el.value);
alert (v);
for(var i = start, e = form.elements; i <= end; ++i) {
if('checkbox' == e[i].type) {
e[i].checked = v;
}
}

This function ticks the boxes, but I still can not untick them...

TIA

- Nicolaas
Jul 23 '05 #6
WindAndWaves wrote:
"RobG" <rg***@iinet.net.auau> wrote in message
news:sL*****************@news.optus.net.au...
WindAndWaves wrote:
[...]
That definitely works! The only thing I was wondering is how I pass the
value of the tickbox that calls the function (i.e. if the user ticks the
new
Zealand wide box then all the regions will be ticked and vice versa)

Thank you - you are a legend (as noted in a recent post).


You can either pass this.value or pass this when calling the function
from the onclick event. If you just pass this, you can get its value
from inside the function as below:

<script type="text/javascript">

function doStuff(cb) {

// A bit of debug, remove when at will
alert('You clicked ' + cb.id
+ ' that has a value of '
+ cb.value);

if (cb.checked) {
// do stuff if cb has been checked
} else {
// do stuff if cb has been unchecked
}
}
</script>

...
<LABEL FOR="cb0">Here is the label for cb0
<INPUT TYPE="checkbox" VALUE="0" id="cb0" CLASS="ix"
onclick="doStuff(this);">
</label>
...

You don't need to use the for attribute of the label if the input is
nested as above, whether you do or not is up to you.

--
Rob

I tried that, but it aint working (it must be something stupid)

function tic(form, start, end, el) {
var v = false
if (el.value) {v = true;} else {v = false;}
alert (el.value);
alert (v);
for(var i = start, e = form.elements; i <= end; ++i) {
if('checkbox' == e[i].type) {
e[i].checked = v;
}
}

This function ticks the boxes, but I still can not untick them...

TIA

- Nicolaas

Without commenting on your code (I think it could be tidied up a bit
but I'm in a rush at the moment, sorry) try replacing:

if (el.value) {v = true;} else {v = false;}

with

(el.checked)? v = true : v = false;

or you could replace a whole slab with:
function tic(form, start, end, el) {
var e = form.elements;
for(var i = start; i <= end; ++i) {
if(e[i].type == 'checkbox' ) e[i].checked = el.checked;
}
}

Untested for above reason... gotta run...

Cheers.

--
Rob
Jul 23 '05 #7

"RobG" <rg***@iinet.net.auau> wrote in message news:46Sod.832

function tic(form, start, end, el) {
var e = form.elements;
for(var i = start; i <= end; ++i) {
if(e[i].type == 'checkbox' ) e[i].checked = el.checked;
}
}

Untested for above reason... gotta run...

Cheers.

--
Rob


I just tested it and it seems to be working marvellously... thank you for
that.
Jul 23 '05 #8
On Wed, 24 Nov 2004 14:52:22 +1300, WindAndWaves <ac****@ngaru.com> wrote:
"RobG" <rg***@iinet.net.auau> wrote in message
news:sL*****************@news.optus.net.au...
[snip]
...
<LABEL FOR="cb0">Here is the label for cb0
<INPUT TYPE="checkbox" VALUE="0" id="cb0" CLASS="ix"
onclick="doStuff(this);">
</label>
...

You don't need to use the for attribute of the label if the
input is nested as above, whether you do or not is up to you.


Actually, that's not true. IE won't focus the control unless the LABEL is
linked via the for attribute.

If anything, it's the for attribute that's the most important, however I
read that a browser required the nesting which is why I use both.
Unfortunately I don't know what browser that is, but it's probably an old
one.
I tried that, but it aint working (it must be something stupid)

function tic(form, start, end, el) {
var v = false
if (el.value) {v = true;} else {v = false;}


What is the value of el? Don't forget that string conversion to boolean is
based upon the length of the string. A length of zero is false, whilst
everything else is true. If you're checked for a specific value, it's best
to compare more explicitly.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #9
JRS: In article <Lc******************@news.xtra.co.nz>, dated Wed, 24
Nov 2004 10:45:31, seen in news:comp.lang.javascript, WindAndWaves
<ac****@ngaru.com> posted :

I am sorry to ask so many questions, but unfortunately, you are just such an
awesome source of knowledge that I can not help myself but ask lots. One
day, I hope to be able to answer other people's questions.

Here we go, I have the following function (I made it myself ...!)

function tic(form,a,z){
for(f=a;z;f++){
if(form[f].type=="checkbox"){
form[f].checked=true;
}
}
}


One of your basic problems is that you do not test, in detail, what you
do.

You have a for loop, and loop contents. The first thing to do is to
test that the loop covers the desired range of numbers. It looks to me
as if it will count up indefinitely (or not at all); in javascript, the
for loop is not a true for loop as in Algol 60 and Pascal and basic, but
a disguised while loop, probably as in C. A constant z will not stop
it, though it can prevent it starting.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #10

"Dr John Stockton" <sp**@merlyn.demon.co.uk> wrote in message
[...]
function tic(form,a,z){
for(f=a;z;f++){
if(form[f].type=="checkbox"){
form[f].checked=true;
}
}
}


One of your basic problems is that you do not test, in detail, what you
do.

You have a for loop, and loop contents. The first thing to do is to
test that the loop covers the desired range of numbers. It looks to me
as if it will count up indefinitely (or not at all); in javascript, the
for loop is not a true for loop as in Algol 60 and Pascal and basic, but
a disguised while loop, probably as in C. A constant z will not stop
it, though it can prevent it starting.


[.....]

That makes sense now. Thank you.
Jul 23 '05 #11
Michael Winter wrote:
[...]

You don't need to use the for attribute of the label if the
input is nested as above, whether you do or not is up to you.

Actually, that's not true. IE won't focus the control unless the LABEL
is linked via the for attribute.

[...]

Actually, you're correct - why doesn't that surprise me? The HTML 4
spec infers that implicitly associating the label with the element
without a "for" is OK:

"When absent, the label being defined is associated with the
element's contents."

<URL:http://www.w3.org/TR/html4/interact/forms.html#h-17.9.1>

Which works fine in the browsers I'd tested, provided the label and
element are not split by across table cells.

However, I guess I'd not tested it sufficiently in IE :-(

--
Rob
Jul 23 '05 #12
On Wed, 24 Nov 2004 22:38:18 GMT, RobG <rg***@iinet.net.auau> wrote:

[snip]
However, I guess I'd not tested it sufficiently in IE :-(


IE's such a pain in the ass, isn't it. :D

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #13

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

Similar topics

2
by: Archi3 | last post by:
Im looking for some help or ideas on which way to go with asp page I am developing.. I have a page with 5 different Main Category Dropdowns, and each of the 5 main categories, can have at least...
0
by: Leigh | last post by:
I am building a data entry application using Java servlets. I had hoped to use drop down boxes to provide the user with data entry selections pulled from a database, but am now questioning, given...
9
by: Dustin | last post by:
Here's what I am trying to do: (Names represent CSS classes.) I want to create a photo gallery. I want the entire gallery to be surrounded by a box named PhotoGallery. I want a fluid placement...
7
by: Griff Miller | last post by:
Please see http://home.houston.rr.com/gmiller15/css/vertprob.html . In mozilla 1.6/1.7 it looks the way I want it, with a thin separation between the two boxes. In IE6, the two boxes touch, which...
8
by: Galina | last post by:
Hello I have 6 dependent list boxes on my ASP page:  Faculty;  Lecturer;  Course;  Course occurrence;  Group;  Week commencing date. When faculty is selected, lists of lecturers and...
13
by: amykimber | last post by:
Hi all, I know I'm doign something really daft, but I can't get this to work... I have a form with a bunch of inputs called ship_owner - why the ? Because I'm submitting this page though php...
4
by: teknoshock | last post by:
I have created a page with multiple drop down boxes, all populated with the same options. My problem is, for 12 dropdown boxes and 40 choices per box, I end up with a massive file. Also, if I...
6
by: dave8421 | last post by:
Hi, I'm a bit confused about the definition about "Prinicpal Block Boxes" in CSS 2.1 draft specification. ( http://www.w3.org/TR/2006/WD-CSS21-20061106 ) <pre> 9.2.1 Block-level elements and...
8
by: Wingot | last post by:
Hey, I have a program I am trying to write using Visual C#, SQL Server 2005/2008, and Visual Studio 2008, and one part of it includes a Schema called Client. Inside this schema, three tables...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.