jQuery Mask does not work on elements created dynamically

Asked

Viewed 638 times

1

I’m using the jQuery Mask to format currency in a form, and in this form it is possible to add several values dynamically and for this I duplicate the div that contains the input, only when duplicating this div jQuery Mask stops working in this new input created, how can I solve this?

Structure:

<div id="payment_draft">
    <div class="form-group col-xl-6">
        <label for="paid_value">Valor Pago</label>
        <input type="text" class="form-control money_format" name="paid_value">
    </div>
</div>

<button class="btn_add_payment">Duplicar</button>

<script type="text/javascript">

    $('.money_format').mask('#.##0,00', {reverse: true});

    $(document).on('click', '.btn_add_payment', function(){

        var payment = $('#payment_draft');
        var container = $('.payments_container'); //adiciona em outra div

        var copy = payment.clone();

        container.append(copy)
    });

</script>

2 answers

2


Use the form below to apply the plugin automatically when the field gains focus, including fields cloned or dynamically added:

$(document).on("focus", ".money_format", function() { 
   $(this).mask('#.##0,00', {reverse: true});
});

See functioned:

$(document).on("focus", ".money_format", function() { 
   $(this).mask('#.##0,00', {reverse: true});
});


$(document).on('click', '.btn_add_payment', function(){

   var payment = $('#payment_draft');
   var container = $('.payments_container'); //adiciona em outra div
   
   var copy = payment.clone();
   
   container.append(copy)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.12/jquery.mask.min.js"></script>
<div class="payments_container">
   <div id="payment_draft">
       <div class="form-group col-xl-6">
           <label for="paid_value">Valor Pago</label>
           <input type="text" class="form-control money_format" name="paid_value">
       </div>
   </div>
</div>
<button class="btn_add_payment">Duplicar</button>

Note: when cloning the div, you are doubling the id="payment_draft". An id should be unique on the page. I suggest changing for class="payment_draft".

  • It worked perfectly! The "payment_draft" I put only here of example same, but it is not so there no, thanks!

1

Cloning an HTML won’t clone the eventListener pointing to the elements of this HTML.

How the mask of jQuery is a eventListener, you need to assign it to the cloned elements as well:

$(document).on('click', '.btn_add_payment', function(){

    var payment = $('#payment_draft');
    var container = $('.payments_container'); //adiciona em outra div

    var copy = payment.clone();
    copy.find('.money_format').mask('#.##0,00', {reverse: true});

    container.append(copy)
});
  • Thank you, it worked, but there is some way to automatically assign?

  • Clone with payment.clone(true) should clone the listeners, but do not know how is the implementation of the mask you are using. I do not guarantee that will work.

  • Automatic in which way?

  • I used clone(true) and clone(true, true) which I saw would be "deep" and also did not work

  • @Maurydeveloper, automatic in the sense that Mask’s EventListener is "dynamic", any new element created with the class "money_format" would already work, it is possible?

  • Yes. But where do you want to create in html?

  • @Maurydeveloper, it has to work anywhere, Sam’s answer already solves that

  • I would add event on instead of focus.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.