Checking if a slider is going up or down
Hey all,
I have three criteria,shown as percentages, that I want to add up to no more than 100%.I am using three sliders to represent the three different criteria,each slider is attached to a textbox which displays the value of the slider. My idea was to make a function that would check the three values of the textboxes and if they were greater than 100 I would return false, otherwise return true. I would use this function during the slider event and return false if the function returned false, thus canceling the event. :
- function checkValues()
{
var total;
var one = parseInt($("#one_value").val());
var two = parseInt($("#two_value").val());
var three = parseInt($("#three_value").val());
total = one + two + three;
if(total < 100)
{
return true;
}
else
{
return false;
}
}
Then when I create the sliders :
- $(".slider").each(function(){
//Textbox associated with the slider
var text = "#" + $(this).attr("id") + "_value";
var value = parseInt($(text).val());
$(this).slider({
range:'min',
value:value,
slide: function(event,ui){
if(checkValues())
{
$(text).val(ui.value);
}
else
{
return false;
}
}
});
});
This works but not quite as intended as once the totals get to 100 they won't move. I realize this is because I am not checking to see if the slider is going up or down, really this check should only be made if the slider is moving up.
Could anyone suggest how I could do this? Is there a way to check which way the slider is moving or if the value is increasing or decreasing? I looked at the documentation but couldn't find anything. I would eventually want to make the whole thing always total 100 and move the other two up or down based on how the one being moved is going, this is of course much more complicated, but if anyone has any suggestions on how I would achomplish that I would like to hear it
. Any advice anyone can give is very much appreciated, thanks!
Jstall