-1

I'm trying to get the background-image: url('XXX');

this is how I'm getting all the inline css

Can somebody help me to get only the url of the background-image. Thanks

$('.c-image').click(function() {
  var x = $('.colored').attr('style');
  console.log(x)

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="c-image">
  <span class="c-background">Outer Span
    <span class="colored" style= "background-size: 100%; background-repeat: no-repeat; background-image: url('0963870.jpg'); ">Inner Span</span>
  </span>
</div>
Alban denica
  • 125
  • 7

1 Answers1

1

To get url address without quotes use css() method and method replace().

$(".colored").click(function () {
    let cleanUrl = $(this).css("background-image").replace('url("', "").replace('")', "");
    console.log(cleanUrl);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="c-image">
    <span class="c-background">
        Outer Span
        <span class="colored" style="background-size: 100%; background-repeat: no-repeat; background-image: url(0963870jpg);">Inner Span</span>
    </span>
</div>
s.kuznetsov
  • 14,870
  • 3
  • 10
  • 25