...

View Full Version : populating an array with checkbox name values...



jamescover
07-07-2004, 10:08 PM
Hi:

I have 20 checkboxes (just 3 showing in the sample below), and I want to determine which out of the 20 have actually been checked, then store their name values in an array. At least, I think that is the way to go, but I'm open to suggestions.


function checkForm(){

var mCB = new Array(myForm.img1Chkbx,myForm.img2Chkbx,myForm.img3Chkbx)
for (i=0;i<mCB.length;i++){
if(mCB[i].checked){
n = mCB[i].name;
var mCBc = new Array(n);
alert (mCBc);
}
}
}


I thought that I might loop through the boxes array (mCB) then use "checked" to get the output I needed, but as you can see (above), I didn't get very far...

I want to store all name values for all checked boxes. Then I want to be able to access that information, so that I can produce output based on each box checked...how might I do that???

BTW, there are about 150 form elements total (The above function is just one out of a larger script, so the elements array is out). The basic script is itself a "script generator" for an image preloading script, made to accomodate up to 20 images. The script will take the value of each image (only the boxes that are checked), and write the anchor tags and popup script for them.

Here is a link to the popup script:

http://www.ekigroup.com/javascript/working.html

And below is a link to the script generator (thread topic), that will produce the script and links for the popup preloader script:

http://www.ekigroup.com/javascript/workGenForm.html

I'm not looking for someone to write the script for me, just a push in the right direction, i.e., how to populate a new array with checkbox name values, then how to extract that information for future application.


One final note: The script has to be Javascript 1.3 compliant. I didn't have much success with the push() method.

Any and all help appreciated. Thanks.




-james

Willy Duitt
07-08-2004, 12:59 AM
I do not understand why you need to store all of the checkbox names when they can be accessed any time you want....

Perhaps if you explained what you are trying to do, someone could suggest a better way of reaching your objective.

But the push() method should work unless you have the variable you are using to store these names within your loop and keep overwriting it on each loop.

But it still doesn't make sense to me.....

.....Willy

jamescover
07-08-2004, 03:44 PM
Hi Willy:

Thanks, again, for taking the time to respond! It's appreciated.

Sorry, if I didn't do a good job explaining my objective: I just wanted a way of checking 20 different checkboxes to see whether or not they were checked, then generating output (links) based upon the values of each corresponding input/text box. So, if someone only checked 10 out of 20 boxes, I would need to output only links for 0-9, etc., in ascending order, because each checkbox both corresponds to a variable value (n) and to the image array index in the main script.

Anyway, I think I got it sorted. In fact, your comment got me headed in a different direction, which ended up being pretty simple (I think). I was just trying to avoid 20 conditionals.

Here's what I came up with so far:

<script>
<!--

function getVars(){

var x = ("");
var a = ("<a href=\"javascript:Link1\"><\/a>\n\n");
var b = ("<a href=\"javascript:Link2\"><\/a>\n\n");
var c = ("<a href=\"javascript:Link3\"><\/a>\n\n");
var d = ("<a href=\"javascript:Link4\"><\/a>\n\n");
var e = ("<a href=\"javascript:Link5\"><\/a>\n\n");

with (myForm){

if (img1Chkbx.checked){
img1Chkbx.value = a;
x += a;
}

if (img2Chkbx.checked){
img2Chkbx.value = b;
x += b;
}

if (img3Chkbx.checked){
img3Chkbx.value = c;
x += c;
}

if (img4Chkbx.checked){
img4Chkbx.value = c;
x += d;
}


if (img5Chkbx.checked){
img5Chkbx.value = c;
x += e;
}

myArea.value = (x);

}
}

//-->
</script>

Later, I'll plug in the values derived from the input boxes for the image file names and height & width parameters.

I created a test page with just 5 links:

http://www.ekigroup.com/javascript/hndlrs.html

Thanks, again, for your help.




-james

Vincent Puglia
07-08-2004, 03:51 PM
Willy -- 'ours is not to ask why, only do and die'. Besides I've been playing with associative arrays again :D



<form>
<input type='checkbox' name='chk1' onclick='fillIt(this)'>
<input type='checkbox' name='chk2' onclick='fillIt(this)'>
<input type='checkbox' name='chk3' onclick='fillIt(this)'>
</form>

var theArray = new Array()
function fillIt(oChk)
{
if (oChk.checked)
{
theArray[oChk.name] = 1;
}
else
theArray[oChk.name] = null;

for (i in theArray)
if (theArray[i])
alert(i)
}

Vinny

jamescover
07-08-2004, 04:10 PM
Hi Vincent:


Thanks for responding. I already had something similar in functionality to your example, but I appreciate your input. In the end, I decided to abandoned the whole array route. It was good for evaluating the boxes, but I wanted to populate a new array with the results.


function checkForm(){
with (myForm){
for(i=0;i<elements.length;i++){
if (elements[i].checked){
eName = elements[i].name;
alert(eName);

}
}
}

Well, it's a work in progress, and I'm learning from everyone's generous help.


Thanks, again.


-james

Vincent Puglia
07-08-2004, 04:20 PM
No problem; glad you worked it out.

see you around, Willy :)

jamescover
07-08-2004, 04:41 PM
oops...a slight mod for NN4.0


<script>
<!--

function getVars(){

var x = ("");
var a = ("<a href=\"javascript:Link1\"><\/a>\n\n");
var b = ("<a href=\"javascript:Link2\"><\/a>\n\n");
var c = ("<a href=\"javascript:Link3\"><\/a>\n\n");
var d = ("<a href=\"javascript:Link4\"><\/a>\n\n");
var e = ("<a href=\"javascript:Link5\"><\/a>\n\n");
var doc = document.myForm;

if (doc.img1Chkbx.checked){
doc.img1Chkbx.value = a;
x += a;
}

if (doc.img2Chkbx.checked){
doc.img2Chkbx.value = b;
x += b;
}

if (doc.img3Chkbx.checked){
doc.img3Chkbx.value = c;
x += c;
}

if (doc.img4Chkbx.checked){
doc.img4Chkbx.value = c;
x += d;
}


if (doc.img5Chkbx.checked){
doc.img5Chkbx.value = c;
x += e;
}

doc.myArea.value = (x);

}

//-->
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum