how to do this

Discussion in 'Web Design, HTML And CSS' started by prashantSum, Nov 16, 2005.

  1. prashantSum

    prashantSum New Member

    Joined:
    Oct 24, 2005
    Messages:
    57
    Likes Received:
    0
    Trophy Points:
    0
    I am using a popup window
    in this I may be having n number of input checkboxes(here I have taken 4)
    like this <input type='checkbox' name='comments1' value='0' />
    so I will be naming it comments1, comments2, and so on.....

    and now to return it's values to parent window I am using this function

    function Done() {

    if (comments1.checked)
    MyArgs[1][0] = 1;
    else
    MyArgs[1][0] = 0;

    if (comments2.checked)
    MyArgs[1][1] = 1;
    else
    MyArgs[1][1] = 0;

    if (comments3.checked)
    MyArgs[1][2] = 1;
    else
    MyArgs[1][2] = 0;

    if (comments4.checked)
    MyArgs[1][3] = 1;
    else
    MyArgs[1][3] = 0;

    window.returnValue = MyArgs;
    window.close();
    }

    now can do something like
    if ( comments + i . checked) (not exactly like this )
    I want to put it in a loop until the length of comments ( that is may be 1 .. 10, length I can determine) .
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    You can loop through the form elements and use a bit of regular expression to get as many checkbox values you want.

    Example:
    Code:
    var myArgs[1] = {};
     fucntion Collect(oForm)
     {
       var e=oForm.elements;
       for(var i=0;i<e.length;i++)
       {
     	if(e[i].type="checkbox" && e[i].name.match(/^comments([\d]+)$/))
     	{
     	  if(e[i].checked)
     		myArgs[1][RegExp.$1] = 1;
     	  else
     		myArgs[1][RegExp.$1] = 0;
     	}
     }
     
     //calling the function
     Collect(document.forms[0]);

    Hope this is useful.


    http://spradeep.blogspot.com
     
  3. prashantSum

    prashantSum New Member

    Joined:
    Oct 24, 2005
    Messages:
    57
    Likes Received:
    0
    Trophy Points:
    0
    thx for reply pradeep,

    actually I have completed that task by some other way,

    but what you have given is intresting I would like to work on it later...and ask any doubts

    one immediate for me when I am looking at script is ( as not aware of RE in java script)
    ' RegExp.$1' do I need to place it as it is or is there any to I need to replace there..
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice