miskellaneousness miskellaneousness - 6 months ago 43
Javascript Question

How can I use toFixed() with this line of code in which two objects are mathematically linked?

I'm sure this is a simple question but I'm completely unfamiliar with jQuery and am just looking to use it for one particular thing. I've come up with linked "zero sum" sliders that change an output value, but this value is often not a whole integer as its inputs may be divided by uneven numbers. I'd the output to be a whole number (either rounded, or simply appearing only to two digits). In searching how to do this, I came across the toFixed() function but I'm not sure how to implement it in my line of code.

Here's the jfiddle: http://jsfiddle.net/7e8xwjer/3/

Below is the code in question:

change: function(event, ui) {
$('#p1').text($("#slider_caya").slider("value"));
$('#p2').text($("#slider_charity").slider("value"));
$('#p3').text($("#slider_artists").slider("value"));
$('#p4').text($("#slider_maps").slider("value"));
$('#p5').text($("#slider_stars").slider("value"));
$('#p6').text($("#slider_comps").slider("value"));
$('#p7').text($("#slider_caya").slider("value") + $("#slider_artists").slider("value")/3 + $("#slider_charity").slider("value"));
}


"#p7" is the output I'd like to appear as a whole integer.

Answer

First do the sum after use toFixed()

$('#p7').text(($("#slider_caya").slider("value") +   
  $("#slider_artists").slider("value")/3 +
  $("#slider_charity").slider("value")).toFixed(2));

http://jsfiddle.net/7e8xwjer/4/

Comments