0

My question is simple but I can't seem to find the solution. I'm changing my html through javascript. I have a header with an image that I want to change every 3 seconds or so. I made an array with the name of the images I want in my cycle.

For changing the image I made a variable with the name of the image. I then try to insert the value of the string into the follow statement:

 imageParent.style.backgroundImage = "url('images/"nextImage".jpg')";

But as you see this is completely the wrong syntax. What is the correct syntax for this?

2
  • 3
    "url('images/" + nextImage + ".jpg')". Commented Jun 23, 2013 at 16:31
  • Thank you! I tried without the spaces, but it didnt work. I was experiencing a bug due to the fact that it was adding .jpg while the files already had a .jpg extension given in the array Commented Jun 23, 2013 at 16:35

2 Answers 2

3

What you're trying to do is known as string concatenation. In JavaScript it is most easily done using the + operator:

"url('images/" + nextImage + ".jpg')"

See The + Operator Used on Strings at http://www.w3schools.com/js/js_operators.asp.

Sign up to request clarification or add additional context in comments.

Comments

0

try maybe +nextImage+ instead of nextImage

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.