0

I have 5 divs with the following ID's;

- slide0
- slide1
- slide2
- slide3
- slide4

I'd like to select them using a variable and id.

var clk = 0;

The clk is incremented each time the function is ran.

$('slide' + clk).fadeOut()<-- How do I make this work?

I'm new to jQuery and in javascript it would have been as simple as;

document.getElementById('slide' + clk).style

4
  • 1
    you missed # $('#slide'+clk).
    – BuDen
    Commented Jan 24, 2014 at 11:28
  • Note that jQuery is JavaScript.
    – Boaz
    Commented Jan 24, 2014 at 11:31
  • @Boaz you're missing library :). it is javaScript library Commented Jan 24, 2014 at 11:33
  • @VedantTerkar Both statements are not contradictory :)
    – Boaz
    Commented Jan 24, 2014 at 11:34

3 Answers 3

5

you're missing #

try

$('#slide'+clk).fadeOut();
2
  • Oh bloody hell that's embarassing! Thanks!
    – user2200184
    Commented Jan 24, 2014 at 11:33
  • if you want to select elements by className use : $("."+className+"");. I'm glad that i could help you. Commented Jan 24, 2014 at 11:35
1
for (i = 0; i < 5; ++i) {
   $('#slide'+i).fadeOut();
}
0
0
$(document).ready(function () {
            var clk = 0;
            for (var i = 0; i < 5; i++) {
                $('#slide' + clk).fadeOut();
            }
        });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.