Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 375763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:32:01+00:00 2026-05-12T14:32:01+00:00

I had a requirement to copy all fields from one form to another similar

  • 0

I had a requirement to copy all fields from one form to another similar form. So instead of copying field by field I wrote a routine, form2form, using jQuery.

function form2form(aF1, aF2) { 
 var selection = "#" + aF1 + " .copy";
 $(selection).each(function() {
     document.forms[aF2].elements[$(this).attr('name')].value = $(this).val();
   });           
}

the routine works. The routine requires that in input form field have a class of copy otherwise I don’t know how to get all fields in the form. (“#form :input”) skips the radio button and select fields.

So my questions are.

Is there a built in function so I didn’t need this?
Is there a better way to write the selection?
How do I modify the routine not to need the class.
Can I pass form objects rather then the form name as a text?
Is there a better way in general?

this is a full page that works:

<!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>test form2form in jQuery</title>
<script type="text/javascript" src="../scripts/jquery.js"></script></head>
<script type="text/javascript">
function form2form(aF1, aF2) { 
 var selection = "#" + aF1 + " .copy";
 $(selection).each(function() {
     document.forms[aF2].elements[$(this).attr('name')].value = $(this).val();
   });           
}
function testform2form () {
 form2form ('form1', 'form2' );
}
</script>
</head>
<body>
<h3>Copy form to form</h3>
<form id="form1" name="form1">
form1: f1 <input type="text" name="f1" id="f1" class="copy" value="from A"/>
f2 <input type="text" name="f2" id="f2" class="copy" value="from B" />
 <select name="fruit" id="fruit" class="copy" >
  <option value="valApple" selected="selected">Apple</option>
  <option value="valOrange">Orange</option>
 </select>
</form>
<form id="form2" name="form2">
form1: f1 <input type="text" name="f1" id="f1" class="copy" value="target A" />
f2 <input type="text" name="f2" id="f2" class="copy" value="target B" />
 <select name="fruit" id="fruit" class="copy" >
  <option value="valApple">Apple</option>
  <option value="valOrange" selected="selected">Orange</option>
 </select>
</form>
<p><a href="#" onclick="testform2form()">Copy Form to Form (form2form)</a></p>
</body>
</html>
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:32:02+00:00Added an answer on May 12, 2026 at 2:32 pm

    > Is there a built in function so I didn’t need this?

    Not really. There’s clone(), but that’s more for copying an element to another spot in the DOM. You need to populate a second form.

    > Can I pass form objects rather then the form name as a text?

    Yes:

    function form2form(formA, formB) {
        $(':input[name]', formA).each(function() {
            $('[name=' + $(this).attr('name') +']', formB).val($(this).val())
        })
    }
    

    You can make it a plugin too!

    (function($) {
        $.fn.copyNamedTo = function(other) {
            return this.each(function() {
                $(':input[name]', this).each(function() {
                    $('[name=' + $(this).attr('name') +']', other).val($(this).val())
                })  
            })
        }
    }(jQuery))
    

    ps the ":input" pseudo selector does not skip radio or select inputs. It explicitly includes select and input elements (at least in 1.3.2), of which radio buttons are a part of.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had a requirement where I need to clear off the select all checkbox
Had a good read through similar topics but I can't quite a) find one
I had to make an Application that displays a form to search data from
Hi I had a similar requirement as below and was curious to know to
I had a requirement where in the text field the first character should be
Its all about N-Tier architecture. In my web application I had an requirement to
I was wondering if someone has had a similar requirement before. What they want
Hii, I had a requirement of encode a string provided to a unreadable format
I am working on a database application in C#. I had a requirement to
I had implemented a custom Gallery with custom Adapter. My Requirement is that however

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.