0

I have a div that can be filled with 3 different layouts for entering dates and times.

This is the html that gets loaded by one of the layouts:

 <div class="row clearfix input-field">
  <div class="span10 date-selection">
    <div class="input-prepend date-time">
     <input class="date-input" name="date" size="16" type="text" value="">
    </div>
    <div class="input-prepend date-time">
     <input class="time-input" name="time" size="16" type="text" value="">
    </div>
  </div>
 </div>

I then use live to manage the listeners like this

$('.date-input').live 'click', (e) ->
    $(this).datepicker({showOn:'focus'}).focus()
$('.time-input').live 'click', (e) ->
  $(this).timepicker({showOn:'focus'}).focus()

This works fine but I also have a link to create a new set of date and time fields.

$(".add-lesson").live 'click', (e) ->
    e.preventDefault()
    lesson = $('.multiple-lessons .row:first').clone()
    $(".multiple-lessons .row:last").after(lesson)
    $(".multiple-lessons .row:last input.date-input").attr('name', 'date2')
    $(".multiple-lessons .row:last input.date-input").attr('id', 'new-date-id')
    $(".multiple-lessons .row:last input.time-input").attr('name', 'time2')
    $(".multiple-lessons .row:last input.time-input").attr('id', 'new-time-id')

In the final code I will be generating better names that are valid for saving in rails so the names and ids above are just for testing.

THE PROBLEM

The pickers work with the original live elements but not the cloned ones. Strangely if I don't use the picker and add loads of new rows using clone all the cloned rows work with the pickers.

I am using the latest jQuery and jQuery ui

Any ideas?

2
  • I thought live() was gone from the latest jQ, use on() instead. Commented Feb 29, 2012 at 16:19
  • Thanks, I swapped it over to .on but now the picker doesnt work when you add the cloned inputs then use the pickers Commented Feb 29, 2012 at 16:47

4 Answers 4

1

There is overloaded version of clone which goes like -

.clone( [withDataAndEvents] [, deepWithDataAndEvents] )


You can bind events normally with .bind() as,

 $('.date-input').bind 'click', (e) ->
    $(this).datepicker({showOn:'focus'}).focus()
$('.time-input').bind 'click', (e) ->
    $(this).timepicker({showOn:'focus'}).focus()


and then use .clone(true, true) as,

lesson = $('.multiple-lessons .row:first').clone(true, true)


I think this will solve the issue.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, when I try to open a cloned picker it opens the original input picker?
In that case, directly binding the datepicker/timpicker would help -- $('.date-input').datepicker({showOn:'focus'}).focus()
1

Below is the fully working solution:

    $(function(){
    var counter = 0;
        $(".add-lesson").live( 'click', function (e) {
            e.preventDefault()
            lesson = $('.multiple-lessons .row:first').clone(false, false);

            $("input.date-input",lesson).attr('name', 'date2'+counter);
            $("input.date-input",lesson).attr('id', 'new-date-id'+counter);
            $("input.time-input",lesson).attr('name', 'time2'+counter);
            $("input.time-input",lesson).attr('id', 'new-time-id'+counter);
            $(".multiple-lessons .row:last").after(lesson);
            $('.multiple-lessons .row:last #new-date-id'+counter)
                .datepicker({showOn:'focus'});
            $('.multiple-lessons .row:last #new-time-id'+counter)
                .timepicker({showOn:'focus'});
            counter++;
        });

        $('.date-input').click(function(){
            $(this).datepicker({showOn:'focus'});
        });
        $('.time-input').click(function(){
            $(this).timepicker({showOn:'focus'});
        });

    });


<div class="multiple-lessons">
 <div class="row clearfix input-field">
     <div class="span10 date-selection">
     <div class="input-prepend date-time">
         <input class="date-input"  size="16" type="text" value=""></input>
     </div>
     <div class="input-prepend date-time">
         <input class="time-input"  size="16" type="text" value=""></input>
     </div>
     </div>
 </div>
</div>

Comments

1

I couldn't get the solution from @pawar to work so I am using this hack until I find a better solution.

$('.date-input').datepicker('destroy')
$('.date-input').datepicker()

Comments

0

When the datepicker is attached to an input, a special class "hasDatepicker" is added to the input to tell the plugin that this input already has a datepicker attached to it. Remove the class from the cloned inputs and then datepicker will correctly bind to them when called.

Something like :

lesson = $('.multiple-lessons .row:first').clone().removeClass('hasDatepicker');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.