0

I have a jquery event which creates dynamic HTML input texts. And inside that loop, I need to show a calender. So my js codes are as below:

$('.nof').keyup(function(){
    $('.myfld').empty();
    var html = '';
    nof = $('.nof').val();
    html = html + '<div class="partners">';
    for(var x = 1;  x <= nof; x++) {
        html = html + '<div class="form_box_2">';  
        html = html + '<div class="applicantform_row"><label>Name : </label><input type="text" name="partner_name_'+x+'" /></div>';
        html = html + '<div class="applicantform_row"><label>Sex : </label><input type="text" name="partner_sex_'+x+'" /></div>';
        html = html + '<div class="applicantform_row"><label>Date of Birth: </label><input type="text" class="appdate" id="dob_'+x+'" name="partner_dob_'+x+'" onchange="getAge();" /></div>';
        html = html + '<div class="applicantform_row appage"><label>Age : </label><div id="age_'+x+'"></div> </div>';
        html = html + '<div class="applicantform_row"><label>PAN : </label><input type="text" name="partner_pan_'+x+'" /></div>';
        html = html + '</div>';
        $('.myfld').html(html);
        $(function() {
            $( "#dob_"+x ).datepicker({ /*dateFormat: 'dd/mm/yy', */defaultDate : '1/1/1980',changeYear : true,changeMonth : true });
        });
   }    
   var html_end = '</div>';
   $('.myfld').append(html_end);
});

I am able to produce the HTML textfields nuber entered in the box, but the jquery calender works for the last input field only ! Whats the reason ? Do I need to put the calender code somewhere else ?

DEMO

2 Answers 2

1
$('.myfld').html(html);

is the problem. You are looping and replacing the Html every time. So on the last time your replace all the html. But you only set the datepicker for the last element. Either wait till after the loop to add the html to '.myfld' . Or you can add a class="dob" to your dob elements. Then assign the datepicker to them all at once using the class like:

 $( ".dob" ).datepicker({ /*dateFormat: 'dd/mm/yy', */defaultDate : '1/1/1980',changeYear : true,changeMonth : true });
Sign up to request clarification or add additional context in comments.

2 Comments

Well its working and not working ! The calender is pop up without clicking on the dob text field ! See my updated fiddle
I appended the html $('.myfld').append(html); after the loop and also create a loop for the datepicker function too. And is working now !
0

Here the fiddle where the calendar pops out of the textbox:

fiddle

You have to add dob class to every texbox and then add datepicker function to every dob class.

 $( ".dob" ).datepicker({});

2 Comments

Hi @tilda . well $( ".dob" ).datepicker({}); makes the calendar pop out without clicking on the text field. This is certainly not what I am looking for !
I'm not sure that I understand where do you want calendar to pop up, but can you check my fiddle again? I've accidentally c/p yours instead of mine..

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.