Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8370247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:52:13+00:00 2026-06-09T13:52:13+00:00

For this problem I have a snippet of PHP code that looks in a

  • 0

For this problem I have a snippet of PHP code that looks in a directory and returns a list of files. I then walk through the array and store each file name as a JavaScript variable using echo

Consider the the following example witch does not include the PHP used to generate the image names:

//result of PHP
image1="cow.jpg"  
image2="pig.jpg"
image3="dog.jpg"

imgIndex=0 //global variable that will be incremented

function setBG(image) {
    someElement.style.backgroundImage= "url(/images/" + image + ")"
};

function nextBG(){
    imgIndex++
    currentIMG="image"+imgIndex;
    setBG(currentIMG);
};

When called function nextBG() will not work. But this will:

function nextBG(){
    setBG(image1);
};

I believe the reason it is not working is because the second example is calling the function using the value of the defined variable (cow.jpg). But the first example is calling the function using the string "image1", witch is obviously not a path name for an image.

Is there a way for me to call the function using the variable image1, then image2 and then image3 and so on without it being interpreted as just a string?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:52:14+00:00Added an answer on June 9, 2026 at 1:52 pm

    Make your variables members of an object. Then you can use [] to access the objects members using a string:

    var images = {
      image1 : "cow.jpg",  
      image2 : "pig.jpg",
      image3 : "dog.jpg"
    };
    
    function nextBG(){
        imgIndex++
        currentIMG="image"+imgIndex;
        setBG(images[currentIMG]);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Never thought I'd have this problem :) The following snippet of code works in
I have a problem when I run this snippet of code on the emulator
so I have this problem I have 3 files like 2.php <?php $variable =
I have this snippet of code (AJAX) in jQuery. $.get('someScript.php?lat=' + clickedLatLng.lat() + '&lon='
I've seen that a few instances of this problem have been raised already. However,
I have some problem convert C++ code to PHP. The purpose is I need
Problem: I have web server that was recently compromised. They targeted javascript files. They
For example I have this snippet of html code: ..... <span class=no> 1</span> require
I have some PHP AJAX code that is supposed to validate some parameters sent
NOTE: I have solved the majority of this problem but have run into a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.