GET BACKGROUND-IMAGE FROM CLASS

by bizamajig

CSS

.widthClass {
    width: 570px;
}
.bgimage {
  width: 250px;
  height: 100px;
  background-image: url(https://fakeimg.pl/250x100/f3c150/);
}

JavaScript

//
//	GET IMAGE USING .CSS( PROPERTY ) - STRIP URL()
//
var bizamajig__get__css_property_from_class = function (prop, fromClass) {
 
    var $inspector = $( '<div>' ).css( 'display', 'none ').addClass( fromClass );
    $( 'body' ).append($inspector); // add to DOM, in order to read the CSS property
    try {
        return $inspector.css( prop ).replace( /url\(|\)/g, '' ).replace(/"/g, "");;
    } finally {
        $inspector.remove(); // and remove from DOM
    }
};


// now call it
alert( bizamajig__get__css_property_from_class( 'width', 'widthClass' ) );
alert( bizamajig__get__css_property_from_class( 'background-image', 'bgimage' ) );