Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Daniel Miller
10,380 PointsJQuery changing img source
So I want to make a changing image gallery where the images change ever few seconds, so I tried to butcher someone else reference code and its not working. Can someone explain why. Here is what I put
$(function () {
var backgrounds = [
'url(../img/G1.jpg)',
'url(../img/G2.jpg)',
'url(../img/G3.jpg)',
'url(../img/G4.jpg)'];
var current = 0;
function nextBackground() {
$(".indiesI").attr(
"src",
backgrounds[current = ++current % backgrounds.length]);
setTimeout(nextBackground, 5000);
}
setTimeout(nextBackground, 5000);
$(".indiesI").attr("src", backgrounds[0]);
});
1 Answer
JP Nothard
3,846 PointsHi Daniel,
With jQuery you dont have to specify 'url()' when you are changing the src attribute, so if you remove the url() it will work
var backgrounds = [
'../img/G1.jpg',
'../img/G2.jpg',
'../img/G3.jpg',
'../img/G4.jpg'];
Robert Richey
Courses Plus Student 16,352 PointsRobert Richey
Courses Plus Student 16,352 PointsHi Daniel,
I added markdown to help make the code more readable. If you're curious about how to add markdown like this on your own, checkout this thread on posting code to the forum . Also, there is a link at the bottom called Markdown Cheatsheet that gives a brief overview of how to add markdown to your posts.
Cheers!