Doubt on how to change jquery Mask type

Asked

Viewed 512 times

2

Well I’m using the jquery Mask plugin at input value. The formatting I am using is the following '0,000'. I wondered how to change the formatting to '0,000' when I select KG in select'.

$('.valor').mask('00.000.000,00', {
  reverse: true
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.3/jquery.mask.js"></script>

<input type='text' class='valor' name='valor'>

<select name='categoria' class='select form_campos'>
  <option value='1'>R$</option>
  <option value='2'>KG</option>
</select>

1 answer

6


You can check in the condition change jQuery, and change the mask:

$('.valor').mask('00.000.000,00', {
  reverse: true
});
$("[name=categoria]").change(function() {
  if ($(this).val() == '1') {
    $('.valor').mask('00.000.000,00', {
      reverse: true
    });
  } else {
    $('.valor').mask('0,000', {
      reverse: true
    });
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.3/jquery.mask.js"></script>

<input type='text' class='valor' name='valor'>

<select name='categoria' class='select form_campos'>
  <option value='1'>R$</option>
  <option value='2'>KG</option>
</select>

  • 1

    very good, that’s exactly what I wanted to do. Thank you

  • 2

    @Hugoborges If Lucascosta’s answer solved your problem, don’t forget to mark the answer as a solution for other people to help!

  • 1

    Well remembered, thank you @Juniornunes!

  • 2

    is waiting for the time hahaha

Browser other questions tagged

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