Change point by comma in input value

3

I have a page with several inputs all of type text and with different names and different IDs.

In all of them there is the numeric value with a dot separating the decimals (10.00) and I would like to change that point by a comma (10.00) using JQuery.

So when the page was finished loading the values of the inputs would automatically change the point per comma.

Would anyone know how to write the function?

Thanks in advance.

    
asked by anonymous 17.03.2015 / 16:33

4 answers

6

If you want to change all elements while loading the page you can do the following:

$(document).ready(function(){
    $('input[type="text"]').each(function(){
        var val = $(this).val().replace('.',',');
        $(this).val(val);
    });
});
input{display:block;margin:5px 0;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="text" value="10.00" />
<input type="text" value="20.00" />
<input type="text" value="30.00" />
<input type="text" value="40.00" />
<input type="text" value="50.00" />
<input type="text" value="60.00" />
<input type="text" value="70.00" />
<input type="text" value="80.00" />
    
17.03.2015 / 18:02
1

If you need to display the numbers in the PT-br format, that is, with points in the decimal separator, this inside the input is the most recommended to use a jquery plugin,

MaskedInput link

MaskMoney link

The plugins help because it presents in PT-br (or other format) but sends in the original format, which avoids having to make another conversion to save in a database or to do mathematical operations, in both javascript and server -side after posting.

    
17.03.2015 / 16:51
0

function alteraPonto(valorInput){
    alert("Valor original: " + valorInput.val());
    alert("Valor com virgula: " + valorInput.val().replace(".", ","));
    $(valorInput).val(valorInput.val().replace(".", ","));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><inputtype="text" id="idInput" onchange="alteraPonto($(this))"/>
<input type="text" id="idInput2" onchange="alteraPonto($(this))"/>
    
17.03.2015 / 16:56
0

I'll introduce you to Javascripty's toFixed () and replace () methods:

var num = 5.56789;
var num_com_2_casas = num.toFixed(2);

var res = num_com_2_casas.replace('.', ',');

// resultado 5,57
    
17.03.2015 / 17:40