user2200184
user2200184

Reputation:

jquery id selector with variable

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

Upvotes: 0

Views: 110

Answers (3)

Kaptan
Kaptan

Reputation: 336

$(document).ready(function () {
            var clk = 0;
            for (var i = 0; i < 5; i++) {
                $('#slide' + clk).fadeOut();
            }
        });

Upvotes: 0

Madhur Ahuja
Madhur Ahuja

Reputation: 22661

for (i = 0; i < 5; ++i) {
   $('#slide'+i).fadeOut();
}

Upvotes: 1

Vedant Terkar
Vedant Terkar

Reputation: 4682

you're missing #

try

$('#slide'+clk).fadeOut();

Upvotes: 5

Related Questions