Reputation: 279
I am trying to create a div with textbox on which i want datepicker to display date. So far i am able to dynamically create the div with textbox and datepicker but i am not being able to display the date on textbox. What am i doing wrong here??
Code:
showdate(){
var newDateBoxdiv = $(document.createElement('div')).attr({ class:"EDate", id:"EDate"+i});
newDateBoxdiv.html('<input type="text" id="DisplayTextBox">');
addDateBox();
newDateBoxdiv.insertAfter('.EDate');
}
//Function called inside the above function
addDateBox(){
$('.EDate').find('input[id^="DisplayTextBox"]').each(function () {
$(this).datepicker({
showOn: "button",
buttonImage: "calendar.png",
buttonImageOnly: true
});
$(this).datepicker({dateFormat: 'dd-M-y'});
var dateFormat = $( this ).datepicker( "option", "dateFormat" );
$(this).datepicker( "option", "dateFormat", 'dd-M-y' );
});
Upvotes: 0
Views: 794
Reputation: 11
use .on():
$( selector ).live( events, data, handler ); // jQuery 1.3+
$( document ).delegate( selector, events, data, handler ); // jQuery 1.4.3+
$( document ).on( events, selector, data, handler ); // jQuery 1.7+
Upvotes: 1
Reputation: 2136
Updated code.. Ive noticed some errors of your code including the newDateBoxdiv values. Your textbox DisplayTextBox is not incrementing as well. Try this instead.
showdate(){
var edatenum = $('#increment').val();
edateinc = "EDate"+edatenum; //increment the value of your div
var displaytextbox = "DisplayTextBox"+edatenum; //of course you have to increment your textbox also
$('#increment').val(""+(edatenum+1)); //increment the value of our hidden field
var newDateBoxdiv = $('<div class="EDate" id="'+edateinc+'"> <input type="text" id="'+displaytextbox+'"></div>');
newDateBoxdiv.insertAfter(".your_existing_box");
addDateBox(); //im not sure if this method already works?
}
<!-- outside of script -->
<input type='hidden' id='increment' value='1'>
Upvotes: 1