Doubt how to change the jquery mask type

2

I'm using the jquery mask plugin in the input value. The formatting I am using is the following '0,000'. I would like to know 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><scriptsrc="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>
    
asked by anonymous 08.12.2016 / 19:27

1 answer

6

You can check the jQuery change condition 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><scriptsrc="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>
    
08.12.2016 / 19:33