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 736255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:35:01+00:00 2026-05-14T07:35:01+00:00

Greetings, I have a form with a variable number of inputs, a simplified version

  • 0

Greetings,

I have a form with a variable number of inputs, a simplified version of which looks like this:

<form>
<label for="same">all the same as first?</label>
<input id="same" name="same" type="checkbox" />
<input type="text" id="foo[1]" name="foo[1]" value="" />
<input type="text" id="foo[2]" name="foo[2]" value="" />
<input type="text" id="foo[3]" name="foo[3]" value="" />
<input type="text" id="foo[4]" name="foo[4]" value="" />
<input type="text" id="foo[5]" name="foo[5]" value="" />
</form>

The idea is to tick the #same checkbox and have jQuery copy the value from #foo[1] into #foo[2], #foo[3], etc. They also need to clear if #same is unchecked.

There can be any number of #foo inputs, based upon input from a previous stage of the form, and this bit is giving me trouble. I’m sure I’m missing something obvious, but I can’t get any variation on $('#dest').val($('#source').val()); to work.

Help!

  • 1 1 Answer
  • 0 Views
  • 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-14T07:35:01+00:00Added an answer on May 14, 2026 at 7:35 am

    jQuery will fail to select by id $('#foo[1]') since it includes [ and ], so I’m selecting the first element as $('[id=foo[1]]'). Then getting all next text boxes, then filtering them out if their id attribute does not match foo[<digits>], and then either applying the same value as the first one, or clearing them depending on the checkbox state.

    example

    $("#same").click(function() {
        var first = $('[id=foo[1]]');
        var next = first.nextAll(':text').filter(function() {
            return /foo\[\d+\]/.test(this.id);
        });
        if($(this).is(':checked')) {
            next.val(first.val());
        }
        else {
            next.val('');
        }   
    });​
    

    Although this works, it might just be easier to add classes such as first and rest to the HTML which would make things a lot easier.

    <input id="same" name="same" type="checkbox" />
    <input type="text" id="foo[1]" name="foo[1]" class="first" value="" />
    <input type="text" id="foo[2]" name="foo[2]" class="rest" value="" />
    <input type="text" id="foo[3]" name="foo[3]" class="rest" value="" />
    <input type="text" id="foo[4]" name="foo[4]" class="rest" value="" />
    <input type="text" id="foo[5]" name="foo[5]" class="rest" value="" />
    

    The jQuery code then simplifies to:

    $("#same").click(function() {
        if($(this).is(':checked')) {
            $('.rest').val($('.first').val());
        }
        else {
            $('.rest').val('');
        }   
    });​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings All; I have a LinkedList of type String which contain some words like
Greetings all, I have a simple JQuery function which is bound to an ASP:Button
Greetings! I have some XML like this: <Root> <MainSection> <SomeNode>Some Node Value</SomeNode> <SomeOtherNode>Some Other
Greetings. I have some inputs dynamically added to form. <input name=input_names[] /> When form
Greetings! I have some XML like this: <Root> <AlphaSection> . . . </AlphaSection> <BetaSection>
Greetings, I have a form where employees enter comments in a multiline textbox with
Greetings! I'm doing a Form on ASP.NET MVC 2, in my view I have
Greetings all, I have a question. On my MOSS 2007 dev box, I created
I have pattern like below hi hello hallo greetings salutations no more hello for
greetings all i have a domain class named car and i have an object

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.