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 7436473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:14:43+00:00 2026-05-29T10:14:43+00:00

I am trying to select a certain value from a multi-element background-image property such

  • 0

I am trying to select a certain value from a multi-element background-image property such as

background-image: url(a.jpg), url(b.jpg), url(c.jpg)  

with a single jQuery expression.

The current solution is to replace the complete property with element.css(‘background-image’).

My intent is to enable the user to cycle through a group of background-images for the second url(), while the other background-images stay intact. Is there an easier way than replacing the complete line? I’m not very confident with regexp-replacement, any kind of help would be greatly appreciated.

  • 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-05-29T10:14:44+00:00Added an answer on May 29, 2026 at 10:14 am

    No there isn’t. Even with multiple background images you still only have one attribute background-image. You need to sort out the string value yourself and hand it back. Something like the following will do the trick.

    jsFiddle

    <div id="showcase"></div>
    <button class="changeBG firstBG">change first bg image</button>
    <button class="changeBG secondBG">change second bg image</button>
    <button class="changeBG thirdBG">change third bg image</button>
    
    var backgrounds = [
        ["url11", "url12", "url13"],
        ["url21", "url22", "url23"],
        ["url31", "url32", "url33"],
    ];
    $('#showcase').data('currentBGs', [0,0,0]);
    $('.changeBG').on('click',function(){
        var $button = $(this),
            whichButton,
            whichBGnum,
            bgLength,
            currentBGs,
            bgString,
            bg0,
            bg1,
            bg2;
    
        if( $button.hasClass('firstBG') ){
            whichButton = 0;
        }else if( $button.hasClass('secondBG') ){
            whichButton = 1;
        }else if( $button.hasClass('thirdBG') ){
            whichButton = 2;
        }
    
        currentBGs = $('#showcase').data('currentBGs');
        currentBGnum = currentBGs[whichButton];
        bgLength = backgrounds[whichButton].length;
    
        currentBGnum = ( currentBGnum + 1 ) % bgLength;
        currentBGs[whichButton] = currentBGnum;
        bg0 =  backgrounds[0][ currentBGs[0] ];
        bg1 =  backgrounds[1][ currentBGs[1] ];
        bg2 =  backgrounds[2][ currentBGs[2] ];
        bgString = 'url(' + bg0 + '), url(' + bg1 + '), url(' + bg2 + ')';
    
        $('#showcase').data('currentBGs', currentBGs)
            .css('background-image', bgString);  
    });
    

    This code is not meant to be optimized, but readable. The example pics are silly, they are just whatever browser windows I had open.

    • Keep a JS array of all the pics. This is a multidimensional array, first level represents each part of the background, second level is each option for that background.
    • Write down the current state of the backgrounds, here we just .data().
    • Figure out which button was clicked
    • Get the current state on the display
    • Cycle the appropriate counter
    • Get the new backgrounds
    • Update the state of the display with both the data and the new backgrounds.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to select certain fields from my entity to be used as
I'm trying to select a column from a single table (no joins) and I
I am trying to delete certain key/value pairs from a hash, but I get
I'm trying to show a second select element only if a certain option of
In trying to select elements that have any attributes, the following throws a jQuery
I'm trying to select data from a table defined similar to the following :
I'm trying to select a random 10% sampling from a small table. I thought
I'm trying to select the name field from the author node in an ATOM
I am trying to find out how to allow users to only select certain
I have an Oracle database where I'm trying to select a user field from

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.