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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:19:02+00:00 2026-06-03T05:19:02+00:00

In this code javascript function is taking from[1] as argument in displayDatePicker(‘from[1]’, false, ‘dmy’,

  • 0

In this code javascript function is taking from[1] as argument in displayDatePicker(‘from[1]’, false, ‘dmy’, ‘-‘). When I clone this (second) row using jquery, all my input and select names get incremented but javascript function is still taking from[1] as argument. I want to ask how to change this from[1] to from[2] and so on

<tr>
         <td width="19%" align="right" ><input type="text" id="roomcat" name="roomcat[1]" value="Deluxe" /></td>
         <td width="1%" align="center" >&nbsp;</td>
         <td width="15%" align="left" ><select class="f" name="roomtype[1]" id="roomtype">
            <option value="">Please Select</option>
            <option value="Single">Single</option>
            <option value="Double">Double</option>
            <option value="Triple">Triple</option>
            <option value="Extra">Extra</option>
         </select></td>
         <td width="7%" align="left" ><input type="text" id="cfit" name="cfit[1]" /></td>
         <td width="8%" align="left" ><input type="text" id="cgit" name="cgit[1]" /></td>
         <td width="7%" align="center" ><input type="text" id="rfit" name="rfit[1]" /></td>
         <td width="8%" align="center" ><input type="text" id="rgit" name="rgit[1]" /></td>
         <td width="10%" align="center" >
            <input class="f" style="width:70px" type="text" size="12" name="from[1]" id="from" value="<?php if($mode==1)
            {
            echo $from;
            }
            else
            {
            echo "From";
            }?>" readonly="readonly"  />
            <a href="javascript:displayDatePicker('from[1]', false, 'dmy', '-')"><i.m.g alt="Pick a date" src="js/date.g.i.f" border="0" width="17" height="16" /></a>
         </td>
         <td width="10%" align="center" >
            <input style="width:70px" class="f" type="text" size="12" name="to[1]" id="to" value="<?php if($mode==1)
              {
              echo $to;
              }
              else
              {
              echo "To";
              }?>" readonly="readonly" />
              <a href="javascript:displayDatePicker('to[1]', false, 'dmy', '-')"><i.m.g alt="Pick a date" src="js/date.g.i.f" border="0" width="17" height="16"  /></a>
         </td>
         <td width="15%" align="left" >&nbsp;</td>
       </tr>

Jquery Code is

    $(document).ready(function() {
    $("#addrow").click(function() {
      var row = $('#table2 tbody>tr:last').clone(true);
        row.find("input:text").each(function(){
    this.name = this.name.replace(/\[(\d+)\]$/, function(str,p1){
            return '[' + (parseInt(p1,10)+1) + ']';
        });
        })
    row.find("select").each(function(){
    this.name = this.name.replace(/\[(\d+)\]$/, function(str,p1){
            return '[' + (parseInt(p1,10)+1) + ']';
        });
        })
      row.insertAfter('#table2 tbody>tr:last');
      return false;
    });
  });   
  • 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-06-03T05:19:03+00:00Added an answer on June 3, 2026 at 5:19 am

    Drop the id attributes in your <input>s, you don’t need them and having duplicate ids just leaves you with invalid HTML and strange problems. If you’re using them for styling, use classes instead. If you need them for something else, then you’ll have to fix them when you copy so that you don’t end up with duplicate ids.

    Now we can clean up your cloning a little bit. When you’re cloning your row, you don’t need to parse the number in brackets, you can just ask the table how many rows it has and add one to get the new number:

    var n = $('#table2 tbody>tr').length + 1;
    

    then your replace calls simplify to this:

    this.name.replace(/\[(\d+)\]$/, '[' + n + ']');
    

    Also, you can use a multiple-selector to iterate through the <input>s and the <select>s at the same time:

    row.find('input:text, select').each(...)
    

    You’re changing the same attribute in both cases so there’s no need for two identical loops.

    Using javascript:displayDatePicker(...) in your <a> isn’t necessary. You can use jQuery to bind to clicks on those <a>s by adding a class to them:

    <a class="datepicker">...</a>
    

    and then binding a callback to them using click and a closest/find combination inside the callback will let you find the corresponding <input>:

    $('a.datepicker').click(function() {
        var $input = $(this).closest('td').find('input[type=text]');
        displayDatePicker($input.attr('name'), false, 'dmy', '-'));
    });
    

    You’re using clone so the event handlers on the cloned elements will be copied so you don’t have to mess around with delegate or the dynamic versions of on.

    Here’s a demo with simplified HTML: http://jsfiddle.net/ambiguous/yEaw6/

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

Sidebar

Related Questions

As you may know, when we have this code in Javascript : function getName()
Check out this piece of JavaScript code: (function (w, d) { var loader =
I have this javascript code <Script> function getGroupId(){ var group=document.getElementById(selectedOptions).value; var groupFirstLetter=group.substring(2); } </Script>
Ok, I have this code: <html> <head> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script> <script type=text/javascript> function get()
Hello i have this form filling javascript: function onLine(code,nn) { document.writeform.bericht.value+=code; document.writeform.bericht.focus(); document.writeform.nickname.value+=nn; write1();
I have problems with the following bit of javascript/jquery code: this.droppable = function(){ $('.imageWindow
I have this code: <script type=text/javascript> var maxLength=10; function charLimit(el) { if (el.value.length >
I used this code for sending and returning result. <script type=text/javascript> $(document).ready(function() { $('.special').click(function(){
I have this code running great in ff, opera and chrome: <script type=text/javascript> $(document).ready(function(){
I have this javascript code : function showMyVideos(data) { var feed = data.feed; var

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.