Search code examples
javascriptjquerystring-interpolation

How to use a variable in jQuery?


How to use variable in jQuery? I used the var i, Here's the code:

var i=0;
for (i=0;i<=5;i++){
   $('.slide:eq(i)').delay(3000).fadeOut(500);
}

Thank you.


Solution

  • It doesn't "work" because it's treated like a plain string.

    You need to concatenate with '+'.

    $('.slide:eq('+i+')').delay(3000).fadeOut(500);
    

    You can also use:

    $('.slide').eq(i).delay(3000).fadeOut(500);
    

    which is clearer.