tags:

views:

28

answers:

2

Sorry for such a simple question but I can't seem to find the solution.

I am trying to fade in and out some divs.

Divs have an ID of "div1", "div2", "div3".

My code is:

var Divs = new Array("div1", "div2", "div3");

I want to fade out one div and then fade in the next on top of it.

I have a setinterval that runs every 5 seconds and checked it works.

Inside it is this code:

 $(Divs[1]).fadeOut(1000);
 $(Divs[2]).fadeIn(1000);

However nothing happens when the timer method is ran. Any ideas?

+6  A: 

Identify them by their ID property. The selector has to look like $('#ID').action(args); and I believe your selector would only select tags of type <div1></div1>, <div2></div2> etc

$('#'+Divs[1]).fadeOut(1000);
David
That will do it.
ryanulit
I see, I am getting somewhere now but I can't get my div to fadeout.. any ideas? I'm a total jquery beginner by the way...
SLC
Can I assume your stuff is wrapped in `$(document).ready(function() { <yourcode> })` ? Or can you start another question and post your source?
David
Hmm the code above should work, it fades fine if I put the ID in manually, but I guess it's not reading from my array properly. I'm not using [1] though I am using a variable to store 1. Could that be the reason?
SLC
var CurrentDiv = 1; ... and ... $("#" + Divs[CurrentDiv]).fadeOut(1000);
SLC
I wouldn't think so--the array syntax looks good. Even Divs[x] should produce the correct selector string.
David
I'm an idiot, arrays start from zero...
SLC
Can you try with single quotes for grins and giggles? I have seen it work in the past, but it's unjustified as far as I can tell.
David
All fixed now, thanks for your help, typical me being a noob...:)
SLC
A: 

Your CSS selector is looking for elements by tag name. In order to search by ID you need the # prefix. Here's the full reference:

http://api.jquery.com/category/selectors/

Try this instead:

var Divs = ["#div1", "#div2", "#div3"];
Álvaro G. Vicario
Isn't this effectively the same answer that **David** gave earlier?
patrick dw
yea.. the OP already got it.. :D
Reigel
Yes. Stack Overflow doesn't always notify you that there're new answers while you're composing yours.
Álvaro G. Vicario