Home > Programming > Cloning table rows and jQuery UI Datepicker()

Cloning table rows and jQuery UI Datepicker()

December 17th, 2009 ^Lestat Leave a comment Go to comments

I’m rather new to both javascript and jQuery. I ran into an issue while cloning table rows that had inputs, and jQuery UI datepicker.

The cloned datepicker input would insert the picked date into the original inputs rather than the cloned row.
cloneDatepicker

The inputs in my table rows had no ID’s. So what I found was either jQuery or UI was dynamically creating it’s own DOM id’s for the inputs in the table rows that had the datepicker call. Datepicker also was dynamically inserting a class “hasDatepicker” into the inputs that had the datepicker call.

I resolved this by:
1. Looping through the cloned rows inputs looking for any inputs with the class “hasDatepicker”.
2. Removing the hasDatepicker class from any inputs with that class.
3. Getting that inputs current id, incrementing it by 1, and reassigning it.
4. Re-initializing the datepicker call.

<script type="text/javascript">

$(document).ready(function(){

  // -- Clone table rows
  $(".cloneTableRows").live('click', function(){
    // do the cloning...

    // new rows datepicker need to be re-initialized
    $(newRow).find("input").each(function(){

      // if the current input has the hasDatpicker class
      if($(this).hasClass("hasDatepicker")){
        var this_id = $(this).attr("id"); // current inputs id
        var new_id = this_id +1; // a new id
        $(this).attr("id", new_id); // change to new id
        $(this).removeClass('hasDatepicker'); // remove hasDatepicker class
        $(this).datepicker(); // re-init datepicker
      }

    });

  });

});

</script>

Whats really neat about this particular js, is that you can call it from any table that has an id.

The only thing I have yet to figure out is how to reset the cloned datepicker to use the default class called date format.

Categories: Programming Tags:
  1. March 22nd, 2010 at 17:18 | #1

    Hi ,

    I’ve just hit the same problem in jQuery, (trying to clone/duplicate table rows) so glad I found your blog post! After a bit more refactoring, I streamlined it a bit more. Take a look, see what you think:

    $(‘.hasDatepicker’, newRow).each(function(){ $(this).removeClass(‘hasDatepicker’).removeAttr(‘id’).datepicker( { dateFormat: ‘dd/mm/yy’ } ); });

    Now instead of incrementing the ID, we remove it and let the datepicker re-init take care of that!

    Cheers, Lee.

  2. March 22nd, 2010 at 17:59 | #2

    @Lee Kelleher
    Cool and thanks for the non-spam comment hehe. After posting it I realized I could have made it cleaner. I havent tried your way of doing it. What I am curious about is re-init-ing the datepicker again. Especially on a colorbox form with multiple pickers. Would re-init-ing (hard to spell) cause problems?

  3. scott
    June 15th, 2010 at 02:24 | #3

    cheers guys.

  4. ferry
    October 7th, 2010 at 00:43 | #4

    can i see the full code script?

  5. March 11th, 2011 at 00:26 | #5

    Nice article material Thank’s alot

  6. Live
    May 19th, 2011 at 03:59 | #6

    You know, it’s a bit strange, I’m not here to clone datepicker, but an Invoice with Inventory system.

    I just happen to like your code and its simplicity to be applied to other areas of jquery + php.

    Thanks a lot! Keep up the good work. )

  7. July 28th, 2011 at 00:57 | #7

    really good blog ,It help me a lot .

  8. sad123
    October 19th, 2011 at 22:11 | #8

    Up to 6 hours talk time and 8 days standby time
    Status Display makes it easy to check battery level and Bluetooth connectivity at a glance
    Touch volume control, answer/end button, voice dialing (depending on the phone used)
    Streams music from A2DP-enabled mobile phones
    Connect two Bluetooth enabled devices at the same time with Multiuse capabilities
    Bluetooth 2.1+EDR technology Jabra BT2080 Universal Bluetooth Wireless Headphone

  9. fdgfg
  10. dfgsadfhd
  11. adfghsdf
    October 27th, 2011 at 00:45 | #11

    good post ,thanks for your sharing . Transformers Led Flashlight

  12. November 11th, 2011 at 05:32 | #12

    This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.

  13. December 3rd, 2011 at 02:02 | #13

    I’d been very pleased to locate this great site.I need to to appreciate this brilliant read!! I definitely enjoying every part of it and I maybe you’ve bookmarked to view new belongings you post.
    longchamp

  14. December 13th, 2011 at 03:45 | #14

    I have never read such a lovely post and I am coming back tomorrow to continue reading.

  15. February 1st, 2012 at 03:42 | #15

    This conversation is going no where. It’s lacking the place of a
    good leader to head the things to come out on conclusion.
    I am waiting for ur quick positive reply. Have a nice day,It is very helpful to know about different historical tourist places all the world. Thank you for providing such useful informations.

  1. No trackbacks yet.