0
I’m using the library mask
to format values, percentages, etc. I have a field carga_imposto
(type %), and other carga_valor
(type 0.00), which can be added dynamically via jquery
. To add, it works perfectly, however, the mask only works with the previously loaded items.
When creating a new item, the input
is not formatted.
HTML of the fields:
<div id="box_carga" class="box_carga">
<div class="row form-group">
<div class="col-12 col-md-2">
<input type="text" id="carga_valor" name="carga_valor[]" placeholder="Valor" class="form-control valor" value="">
</div>
<div class="col-12 col-md-1">
<input type="text" id="carga_imposto" name="carga_imposto[]" placeholder="Imposto" class="form-control percentual" value="">
</div>
<div class="col-12 col-md-1">
<a href="javascript:void(0);" class="adicionar_carga"><button type="button" class="btn btn-primary"><i class="fa fa-plus"></i></button></a>
</div>
</div>
</div>
HTML to be cloned:
<div id="box_carga_clone" style="margin-top: 15px;" class="box_carga_clone hide">
<div class="row form-group">
<div class="col-12 col-md-2">
<input type="text" id="carga_valor" name="carga_valor[]" placeholder="Valor" class="form-control valor" value="">
</div>
<div class="col-12 col-md-1">
<input type="text" id="carga_imposto" name="carga_imposto[]" placeholder="Imposto" class="form-control percentual" value="">
</div>
<div class="col-12 col-md-1">
<a href="javascript:void(0);" class="remover_carga"><button type="button" class="btn btn-danger"><i class="fa fa-trash"></i></button></a>
</div>
</div>
jQuery
When creating the field dynamically, I tried to add $('.carga_imposto').mask('##0,00%', {reverse: true});
, but without success:
$('.adicionar_carga').click(function(){
$clone = $('.box_carga_clone.hide').clone(true);
$clone.removeClass('hide');
$('.box_carga').append($clone);
$('.carga_imposto').mask('##0,00%', {reverse: true});
});
$('.remover_carga').click(function(){
$(this).parent().parent().parent().remove();
});
$('.remover_carga_ativo').click(function(){
$(this).parent().parent().remove();
});
Currently the result I have is:
The first was loaded on screen, the second was added dynamically.
Hi. So I tried to make the switch. But it didn’t work.
– Sr. André Baill
Try switching
$('.carga_imposto').mask('##0,00%', {reverse: true});
for$clone.find('.carga_imposto').mask('##0,00%', {reverse: true});
. In its cloned elements, you have to change theid
by class.– Benilson
carga_tax is as id in the elements, should be a class.
– Benilson
I agree, but it includes the class, but it doesn’t work... technically, it should work.
– Sr. André Baill
No, the system applies anything in the first
id
not applicable to all who have the sameid
.– Benilson
Yes, if it is by class, I should apply in a next class, independent, but, if it is on screen... however, if I add a new element on screen, I need to tell him that he needs to include also in this element, and here’s the problem.
– Sr. André Baill