Reapply the result with a comma after checking or unchecking checkbox values

Asked

Viewed 18 times

1

I know there is replace but can’t apply so that the total is presented with a comma after checking or unchecking the checkbox.

$(function(){
  total = document.getElementById('total');
  $(document.getElementsByName('choice')).bind('click',function(){
	
	var valTotal = total.value;
		
	valTotal = parseFloat(valTotal.replace(',','.'));
		
	var valInput = (this.value);
		
	valInput = parseFloat(valInput.replace(',','.'));
		
	var novoTotal=0;
	total.value = (this.checked) ? parseFloat(( valTotal + valInput )).toFixed(2) : parseFloat(( valTotal - valInput )).toFixed(2) ;
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

<form name="listForm" id="listForm">
 <input type="checkbox" name="choice" value="2,55" />2,55<br/>
 <input type="checkbox" name="choice" value="2,45" />2,45<br/>
 <input type="checkbox" name="choice" value="2,50" />2,50<br/>
 <input type="checkbox" name="choice" value="2,00" />2,00<br/>
 <input type="text" size="4" name="total" id="total" value="100,00"/>
</form>

1 answer

2


Use a replace like this:

total.value = value.replace('.', ',');

Example:

$(function() {
  total = document.getElementById('total');
  $(document.getElementsByName('choice')).bind('click', function() {

    var valTotal = total.value;

    valTotal = parseFloat(valTotal.replace(',', '.'));

    var valInput = (this.value);

    valInput = parseFloat(valInput.replace(',', '.'));

    var novoTotal = 0;
    var value = (this.checked) ? parseFloat((valTotal + valInput)).toFixed(2) : parseFloat((valTotal - valInput)).toFixed(2);
    total.value = value.replace('.', ',');
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


<form name="listForm" id="listForm">
  <input type="checkbox" name="choice" value="2,55" />2,55<br/>
  <input type="checkbox" name="choice" value="2,45" />2,45<br/>
  <input type="checkbox" name="choice" value="2,50" />2,50<br/>
  <input type="checkbox" name="choice" value="2,00" />2,00<br/>
  <input type="text" size="4" name="total" id="total" value="100,00" />
</form>

  • 1

    Fuck, just like that!! Thanks!!

Browser other questions tagged

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