Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript for loop with decimals

I'm trying to use this for loop in order to show divs. But I get a strange error from the jQuery lib.

Error: Syntax error, unrecognized expression: =10]

I have read about the problems with javascript decimals, but I still can't understand why this won't work:

for (var i = 10.00; i >= ui.value; i -= 0.25) {
    $("data_id=" + Math.floor(i) + "]").show();
}

When hiding the divs, I use this and it works fine:

for (var i = 0.00; i < ui.value; i += 0.25) {
    $("[data_id=" + Math.floor(i) + "]").hide();
}
like image 943
8bitcat Avatar asked Dec 20 '22 16:12

8bitcat


1 Answers

You forgot the [ in the first loop, this will work:

for (var i = 10.00; i >= ui.value; i -= 0.25) {
    $("[data_id=" + Math.floor(i) + "]").show();
}

You should transform this into an integer loop, if you are .floor()-ing the numbers, anyway.

like image 102
Gregor Avatar answered Dec 30 '22 18:12

Gregor