SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,211
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    looping function

    Code:
    <script language="javascript">
    
     function copy_tag1()
     {  document.form_tag1.tag.select();
      window.clipboardData.setData('text', 
                document.form_tag1.tag.value); }
    
    function copy_tag2()
     {  document.form_tag2.tag.select();
      window.clipboardData.setData('text', 
                 document.form_tag2.tag.value); }
    
    function copy_tag3()
     {  document.form_tag3.tag.select();
      window.clipboardData.setData('text', 
                document.form_tag3.tag.value); }
    
    function copy_tag4()
     {  document.form_tag4.tag.select();
      window.clipboardData.setData('text', 
                document.form_tag4.tag.value); }
    
    </script>
    
    <form name="form_tag1">
    <input type='text' name="tag" value='1111'>
    <input type=button value="copy" onclick="copy_tag1()">
    </form>
    
    <form name="form_tag2">
    <input type='text' name="tag" value='2222'>
    <input type=button value="copy" onclick="copy_tag2()">
    </form>
    
    
    <form name="form_tag3">
    <input type='text' name="tag" value='3333'>
    <input type=button value="copy" onclick="copy_tag3()">
    </form>
    
    
    <form name="form_tag4">
    <input type='text' name="tag" value='4444'>
    <input type=button value="copy" onclick="copy_tag4()">
    </form>
    The code above make it copy each tag value by clicking each copy button.




    There are four copy_tag functions in the javascript.

    If I have more forms to copy, I have to make more copy_tag functions.


    Can I make the javascript to loop for many many copy forms with your help??

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is a forms[] array that contains all the forms on the page, e.g. document.forms[0], document.forms[1]..., which can also be used with the form names for the index values, e.g. forms["formName1"], forms["formName2"]. It would be a simple matter to create a loop using numeric index values with the forms[] array, or using cleverly chosen form names as index values:
    Code:
    currentFormName = "form" + i;
    document.forms[currentFormName]...

  3. #3
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,211
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud View Post
    Code:
    currentFormName = "form" + i;
    document.forms[currentFormName]...
    a little more detail please...

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    window.onload = function()
    {
    	theForms = document.forms;
    	for(var i=0, len = theForms.length; i<len; ++i)
    	{
    		alert(theForms[i].elements["tag"].value);
    							
    	}
    }

  5. #5
    SitePoint Guru SSJ's Avatar
    Join Date
    Jan 2007
    Posts
    828
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    <script language="javascript">

    function copy_tag(frm)
    {
    frm.tag.select();
    window.clipboardData.setData('text', frm.tag.value);
    }
    </script>

    <form name="form_tag1">
    <input type='text' name="tag" value='1111'>
    <input type=button value="copy" onclick="copy_tag(this.form)">
    </form>

    <form name="form_tag2">
    <input type='text' name="tag" value='2222'>
    <input type=button value="copy" onclick="copy_tag(this.form)">
    </form>
    Hope this will help..

  6. #6
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,211
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much.


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
  •