Change point per comma to input value

Asked

Viewed 18,313 times

4

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

In all of them there is the numerical value with point separating the decimals (10.00) and would like to change this point per comma (10.00) using Jquery.

Thus, when the page finished being loaded the input values would change the point per comma automatically.

Would anyone know how to write the function?

Thank you so much.

  • Hello. Can you explain better? the formatting you need is mask type? i.e., to exchange the input, or is after posting, to save or display the value elsewhere?

  • To really change it. On the page it is loaded with inputs and in its value it has numbers that contain points (.) and I want jquery to change these points by comma (,)

  • 1

    If you want to change when the page loads it seems to me that the best is to do it on the server side... is an option?

  • unfortunately not. It was my first bad option as the page is dynamic the object returned from the server (python work) is not supported to use a for to use an replace

  • Ok... then and you want to change only 1 time or whenever a new point value is typed?

4 answers

7


If you want to change all the 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>
<input type="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" />

1

Being the need to show the numbers in EN format, ie with points in the decimal separator, this within the input itself the most recommended is to use a jquery plugin, I suggest the following:

Maskedinput http://plugins.jquery.com/maskedinput/

Maskmoney http://plugins.jquery.com/maskMoney/

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

  • yes it seemed that it is quite functional even. but there are no problems in sending as "," not despite being written in the database as ".". I really just need you to change the page when it loads.

0

Using Jquery you can use replace to replace a character or even a character set in a String:

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>
<input type="text" id="idInput" onchange="alteraPonto($(this))"/>
<input type="text" id="idInput2" onchange="alteraPonto($(this))"/>

  • works, but I’m looking for something more dynamic because each field has a unique name, class and ID.

  • @Henriquecasagrandeb, response edited

  • @Henriquecasagrandeb you have the possibility to add an extra class just to call once in jquery, e.g..: class="sua-class inputFloat" (...) $('.inputFloat').val($('.inputFloat').val().replace(".", ","))

  • It worked perfectly. Thank you Gê Bender.

  • @Henriquecasagrandeb, if the answer solved your problem you can mark as correct

0

I present you only toFixed() and replace() methods of Javascripty:

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

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

// resultado 5,57

Browser other questions tagged

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