0

I have a problem. I have a dynamic form that can generate a rows of textfields and these textfields are should be datepicker. These textfields have same classes but only the first one has a datepicker. The rest can't generate a datepicker. But when I checked their classes they have the class of datepicker.

Here's my simple code:

function addNewRowCheck() {

        check_html = '<tbody id="check-row' + check_row + '">';
            check_html += '<tr style="background-color: #FDCACB">';
                check_html += '<td class="txt-center"><input type="hidden" name="check[' + check_row + '][order_no]" value="' + check_row + '" /></td>';
                check_html += '<td>';
                    check_html += '<select>';
                        check_html += $select_account_type;
                    check_html += '</select>';
                check_html +- '</td>';
                check_html += '<td><input type="text" class="txt-center chkdate" name="check[' + check_row + '][curr]" /></td>';
                check_html += '<td><input type="text" class="txt-right" name="check[' + check_row + '][rate]" /></td>';
                check_html += '<td><input type="text" class="txt-right" name="check[' + check_row + '][amount]" /></td>';
                check_html += '<td><input type="text" class="txt-right" name="check[' + check_row + '][check_number]" /></td>';
                check_html += '<td><input type="text" class="txt-right chkdate" name="check[' + check_row + '][check_date]" /></td>';
                check_html += '<td><input type="text" class="txt-right" name="check[' + check_row + '][bank_name]" /></td>';
                check_html += '<td class="center"><img id="remove_btn" src="<?php echo $button_remove; ?>" onclick="$(\'#check-row' + check_row + '\').remove()" style="display: inline;" /></td>';
            check_html += '<tr>';
        check_html += '</tbody>';

        $('#check-content tfoot').before(check_html);

        check_row++;

}

....

$('.chkdate').each(function() {
    $(this).datepicker({
        dateFormat: 'yy-mm-dd',
        changeMonth: true,
        changeYear: true
    });
});
2
  • 3
    After adding a new row, you have to set it manually to let it be a datepicker. Your code only initializes the already there to be datepicker, not the dynamically added ones. Commented Oct 14, 2014 at 7:01
  • there is no event delegation kind of way to handle plugin initialization... you need to do it after the new element is created... so var $new = $(check_html).insertBefore('#check-content tfoot'); $new.find('.chkdate').datepicker({ dateFormat: 'yy-mm-dd', changeMonth: true, changeYear: true }); Commented Oct 14, 2014 at 7:06

1 Answer 1

1

* you start jquery click function*

$("input[type=text]").click(function() {

    $(this).datepicker({
        dateFormat: 'yy-mm-dd',
        changeMonth: true,
        changeYear: true
    });
});
Sign up to request clarification or add additional context in comments.

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.