views:

416

answers:

2

I have a problem with datePicker. When a make a new text field with datepicker and append it to some div it wont work.. Did anyone had the same problem...

$(function(){

    $("#DP").datepicker({showOn: 'button', buttonId: 'kalendar', buttonImage: 'calendar.gif', buttonImageOnly: true});

    $('a').click(function(){

         $('<input type="text" id="DP">').appendTo('#some_div').show();   
    });
});
...
...

<\body>
<\div id="some_div"></div>
<\a href="#">New datepicker<\/a>
...


I need changeble numbers of datePickers fields. And sorry about my english..

A: 

Problem is that you are setting "all" (i.e. one) #DP to be datepicker at document.ready, then you create a new input with id #DP. This won't automatically be set to datepicker, you either need to set the new input as datepicker at "constriction time", i.e.

$('a').click(function(){

     $('<input type="text" id="DP">').
         datepicker(/*...*/).appendTo('#some_div').show();   
});

NB! You can only have one DOM element with ID=DP

veggerby
A: 

I think this does what you want it to:

...

  <script type="text/javascript">
    $(document).ready(function(){
     field_count = 0;

     $("#add_datepicker").click(function(){
      field_count++;
      var new_field = jQuery("<input type=\"text\" />")
      new_field.attr("id","datepicker_"+field_count);
      new_field.attr("name","datepicker_"+field_count);
      $('#container').append(new_field);
      $('#container').append(jQuery("<br />"));
      $("#datepicker_"+field_count).datepicker();
     });
  });
  </script>
</head>
<body>

<form>
<input type="button" id="add_datepicker" value="Add Datepicker"/>
<div id="container">

</div>
</form>

...

This allows you to add another <input> element with a unique id and name so that it can be submitted as form data.

Dave Forgac
Thank you both. It's just what I was looking for. Thanks again....