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

  • Home
  • SEARCH
  • 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 8037869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:03:21+00:00 2026-06-05T03:03:21+00:00

I currently have the code below which successfully sets a random image from an

  • 0

I currently have the code below which successfully sets a random image from an array as .wrapper‘s background. I’d like to have another variable (string) that is permanently paired with each specific image so that when an image is randomly selected, its particular string is also passed in. So if foo.jpg is randomly selected, variable x is set to ‘foo’, but if bar.jpg is randomly selected, variable x is set to ‘bar’.

Any ideas how this can be achieved? Possibly with a JSON object?

my code:

var images = ["foo.jpg", "bar.jpg", "baz.jpg", "qux.jpg"];
function getImage() {
        return images[Math.floor(Math.random() * images.length)];
}
$('.wrapper').css({
    'background' : 'url(' + getImage() + ') no-repeat center center fixed'
});

Let me know any questions you may have.

Thanks

  • 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-05T03:03:23+00:00Added an answer on June 5, 2026 at 3:03 am

    You can make your array an array of objects instead where each object in the array contains both the url and the tag. You randomly select one of the objects and can then separately access both the url and the tag:

    var images = [
        {url: "foo.jpg", tag: "foo"}, 
        {url: "bar.jpg", tag: "bar"}, 
        {url; "baz.jpg", tag: "baz"},
        {url: "qux.jpg", tag: "qux"}
    ];
    
    function getNum() {
            return Math.floor(Math.random() * images.length);
    }
    var item = images[getNum()];
    $('.wrapper').css({
        'background' : 'url(' + item.url + ') no-repeat center center fixed'
    }).html(item.tag);
    

    Or, if the tag is always just the base of the image name so you can parse it out of the url, you could do this:

    var images = ["foo.jpg", "bar.jpg", "baz.jpg", "qux.jpg"];
    function getNum() {
            return Math.floor(Math.random() * images.length);
    }
    var num = getNum();
    $('.wrapper').css({
        'background' : 'url(' + images[num] + ') no-repeat center center fixed'
    }).html(images[num].replace(/\.jpg$/, ""));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the code which gets the currently selected image, but I would like
I currently have the code below in my WPF application which does exactly what
I have a python psp page code is shown below. Currently it only prints
Using dotnet 2.0. I currently have code like this : DataView dv = new
I currently have this code which reads the first field in a database record
I currently have some code which when you press the submit button, a marker
Currently I have the code below to put the data in a Hash. My
Currently I have the below jQuery which uses the colorbox plugin http://colorpowered.com/colorbox/ $(document).ready(function(){ $(.altbgcolor).hover(function()
I currently have some haml code which reads as %span.flagb.flag-gb this builds me a
I currently have code that does the following: private final static ExecutorService pool =

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.