Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts

    populating an array with checkbox name values...

    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
    Last edited by jamescover; 07-07-2004 at 10:27 PM.
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Last edited by jamescover; 07-08-2004 at 03:14 PM.
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #4
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Willy -- 'ours is not to ask why, only do and die'. Besides I've been playing with associative arrays again



    <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
    Where the world once stood
    the blades of grass
    cut me still

  • #5
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #6
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    No problem; glad you worked it out.

    see you around, Willy
    Where the world once stood
    the blades of grass
    cut me still

  • #7
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •