@denism300

Как вызвать функцию для клонированного элемента?

Есть форма с полями input, подключен плагин для маски ввода.
<div class="phoneList__items" style="display: flex; align-items: center; margin-bottom: 0.5rem;">
    <div style="border: 1px solid #7e8993;; border-radius: 4px; margin-right: 15px; display: inline-block;padding: 8px;">
        <p><label for="phone-number__0">Телефон компании:</label></p>
        <p><input id="phone-number__0" class="phone-number" size="45" type="text" name="number[0]" value="" placeholder="+7 (999) 999-99-99" /></p>
    </div>
    <span class="dashicons dashicons-plus add-phone-item" style="cursor: pointer;"></span>
    <span class="dashicons dashicons-trash delete-phone-item" style="cursor: pointer;"></span>
</div>

скрипт для клонирования
jQuery(document).ready(function() {
    jQuery('.phone-number').mask('+7 (999) 999-99-99');
    var phoneList = jQuery('.phone-list');
    jQuery('.add-phone-item', phoneList).click(function() {
        phoneItem = jQuery(this).closest('.phoneList__items');
        newItem = phoneItem.clone(true);
        newItem.find('input').each(function() {
            jQuery(this).val('');
            newInputId = jQuery(this).attr('id');
            newInputName = jQuery(this).attr('name');
            jQuery(this).attr({
                id: newInputId.replace(/\d+$/, m => +m + 1),
                name: newInputName.replace(/\d+?/, m => +m + 1)
            });
        });
        jQuery(this).siblings('label').attr('for', newInputId.replace(/\d+$/, m => +m + 1));
        phoneItem.after(newItem);
    });
    jQuery('.delete-phone-item', phoneList).click(function() {
        i = phoneList.find('.insuranceList__items').length;
        phoneItem = jQuery(this).closest('.phoneList__items');
        if (i > 1) {
            phoneItem.remove();
        } else {
            phoneItem.find('input').each(function() {
                jQuery(this).val('');
            });
        }
    });
});

как вызвать функцию маски для склонированного элемента?
  • Вопрос задан
  • 21 просмотр
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы