1
<div style="background: url(http://www.example.com/image.jpg) center center/cover;">

This inline background image gets overridden by an external stylesheet:

div {
  background-image: none !important;
}

My question: using JavaScript/jQuery, how can I get the URL of the background image that was defined in the inline styling but was overridden by CSS?

Edit: Trying to use .css() won't work because that gets the computed background style, which is none

4
  • I saw an answer for this in another SO question. Forgot where. Commented Sep 22, 2015 at 18:38
  • @Manu Nopes. I checked it. This is different. Commented Sep 22, 2015 at 18:38
  • try this var bg = $("div").css('background-image'); bg = bg.replace('url(','').replace(')',''); Commented Sep 22, 2015 at 18:38
  • Check out my answer now... :) Commented Sep 22, 2015 at 19:02

3 Answers 3

1

If you don't mind using Regex :

var match = $('div').attr('style').match(/url\(["|']?(.+)["|']?\)/);
var url = match ? match[1] : 'default.jpg';

Explanation :

["|']? will allow you to have quotes or not. (.+) will capture the url to recover.

On the second line, we check that there is a match, and if there is none, return a default url.

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

3 Comments

Can I ask, why have you escaped " and '? I didn't think they had a meaning in regular expressions?
@BinaryFunt You're right! They don't need to be escaped.
Also, I think making the (.+) into (.+?) is required, otherwise a quote mark could be left on the end? PS nice and simple answer btw!
1

You can get it this way:

function getStyle() {
  styles = $("div").attr("style");
  styles = styles.split(";");
  for (i = 0; i < styles.length; i++) {
    styles[i] = styles[i].replace(new RegExp(/http(.*)\:\/\//), "http$1___//");
    styles[i] = styles[i].split(":");
    styles[i][1] = styles[i][1].replace(/___/g, ":");
    if (styles[i][0].trim().indexOf("background") === 0)
      return styles[i][1].replace(/^(.*)url|[\(\)]/g, '').split(" ", 1)[0];
  }
}

Working Snippet

$(function () {
  alert($("div").css("background-image"));
  alert(getStyle());
});

function getStyle() {
  styles = $("div").attr("style");
  styles = styles.split(";");
  for (i = 0; i < styles.length; i++) {
    styles[i] = styles[i].replace(new RegExp(/http(.*)\:\/\//), "http$1___//");
    styles[i] = styles[i].split(":");
    styles[i][1] = styles[i][1].replace(/___/g, ":");
    if (styles[i][0].trim().indexOf("background") === 0)
      return styles[i][1].replace(/^(.*)url|[\(\)]/g, '').split(" ", 1)[0];
  }
}
div {
  background-image: none !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div style="background: url(http://www.example.com/image.jpg) center center/cover;">

Note: The $('div').css('background-image'); will always give the computed value of none.

Comments

0

use jQuery css method to get the DIV's background-image:

var bg = jQuery('div').css('background-image');

3 Comments

@PraveenKumar elaborate please
I looked around a little, I think this question might have been answered already stackoverflow.com/questions/837694/…
@GrafiCodeStudio That question is different because the background is not being overridden. Your answer here gets the computed background, which is none due to the stylesheet

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.