Results 1 to 3 of 3

Thread: Javascript loop elements

  1. #1
    Join Date
    Jun 2007
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Javascript loop elements

    Trying to accomplish this:

    I have a form with many form elements. I want the value of the input fields that end with CY be transferred to the input fields that end with PY. So form input with name xxxCY will be moved to form input xxxPY, form input with name yyyCY will be moved to form input yyyPY, etc.

    Having trouble formulating the javascript.

    Example:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    <script language="JavaScript">
    
    function movecytopy()
    {
    for(i=0; i<document.FormName.elements.length; i++)
    {
    NEED HELP WITH THIS PART
    }
    }
    </script>
    
    </head>
    
    <body>
    <form id="form1" name="form1" method="post" action="">
    <table width="100" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><input type="button" name="button" id="button" value="MOVE CY TO PY" onclick="movecytopy()" /></td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td><input name="example1CY" type="text" id="example1CY" size="15" /></td>
        <td><input name="example1PY" type="text" id="example1PY" size="15" /></td>
      </tr>
      <tr>
        <td><input name="example2CY" type="text" id="example2CY" size="15" /></td>
        <td><input name="example2PY" type="text" id="example2PY" size="15" /></td>
      </tr>
      <tr>
        <td><input name="example3CY" type="text" id="example3CY" size="15" /></td>
        <td><input name="example3PY" type="text" id="example3PY" size="15" /></td>
      </tr>
      <tr>
        <td><input name="example4CY" type="text" id="example4CY" size="15" /></td>
        <td><input name="example4PY" type="text" id="example4PY" size="15" /></td>
      </tr>
    </table>
    </form>
    </body>
    </html>
    Last edited by lindm; 12-01-2007 at 08:07 AM. Reason: Definition

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>MOVE CY TO PY</title>
    
    <script type="text/javascript">
    function movecytopy(el){
    for (var c, els=el.form.elements, i = els.length-1; i > -1; --i)
    if((c=els[i].name)&&/CY$/.test(c))
    for (var p, j = els.length-1; j > -1; --j)
    if((p=els[j].name)&&c.replace(/CY$/, 'PY')==p)
    els[j].value=els[i].value, els[i].value='';
    }
    </script>
    
    </head>
    
    <body>
    <form id="form1" name="form1" method="post" action="">
    <table width="100" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><input type="button" name="button" id="button" value="MOVE CY TO PY" onclick="movecytopy(this)"></td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td><input name="example1CY" type="text" id="example1CY" size="15"></td>
        <td><input name="example1PY" type="text" id="example1PY" size="15"></td>
      </tr>
      <tr>
        <td><input name="example2CY" type="text" id="example2CY" size="15"></td>
        <td><input name="example2PY" type="text" id="example2PY" size="15"></td>
      </tr>
      <tr>
        <td><input name="example3CY" type="text" id="example3CY" size="15"></td>
        <td><input name="example3PY" type="text" id="example3PY" size="15"></td>
      </tr>
      <tr>
        <td><input name="example4CY" type="text" id="example4CY" size="15"></td>
        <td><input name="example4PY" type="text" id="example4PY" size="15"></td>
      </tr>
    </table>
    </form>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2007
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Works in my initial testing. Wonderful!

Bookmarks

Posting Permissions

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