skip to Main Content

So I have a DIV that contains a background image. In some cases this image is set "directly", in other cases it’s a generated SVG. I want to be able to identify when a DIV contains the SVG-string.

My two DIVs:

<div class="cover my-1" style="background-image: url('something.jpg');">Div 1 QQ</div>
<div class="cover my-2" style="background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc...=');">Div 2</div>

And I try this:

$(document).ready(function() {
  $(".cover.my-1:contains(QQ)").css("background-color", "yellow");
  $(".cover.my-2:contains(svg+xml)").css("background-color", "yellow");  
}); 

So, DIV 1 changes as QQ is found. DIV 2 does not change as "svg+xml" is not found within the DIV-field it self (but is in the style). How do I identify the "svg+xml" in DIV 2?

I was hoping to identify the "svg+xml" within the style of the DIV

4

Answers


  1. To identify when a DIV contains an SVG-string as a background image, you can use JavaScript to check the style attribute of the DIV and look for the "data:image/svg+xml" substring. Here’s an example

    // Get all the DIVs with class "cover"
      var covers = document.querySelectorAll('.cover');
      
      // Loop through each DIV
      covers.forEach(function(cover) {
        // Check if the background image is an SVG
        if (cover.style.backgroundImage.indexOf('data:image/svg+xml') !== -1) {
          // console log if found
          console.log(cover);
        }
      });
    <div class="cover my-1" style="background-image: url('something.jpg');">Div 1 QQ</div>
    <div class="cover my-2" style="background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc...=');">Div 2</div>

    I see you are using jQuery, so here;s the updated code for you

    jQuery

    $('.cover').each(function() {
        // Check if the background image is an SVG
        if ($(this).css('background-image').indexOf('data:image/svg+xml') !== -1) {
          // console log if found
          console.log($(this));
        }
      });
    Login or Signup to reply.
  2. :contains works only for the content of the element. You can use filter and includes to select your items with svg background

    $(document).ready(function(){
      $(".cover.my-1:contains(QQ)").css("background-color", "yellow");
     $(".cover.my-2").filter((_, el) => $(el).css("background-image").includes("svg+xml")).css("background-color", "yellow");  
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="cover my-1" style="background-image: url('something.jpg');">Div 1 QQ</div>
    <div class="cover my-2" style="background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc...=');">Div 2</div>
    <div class="cover my-2" style="background-image: url('something.jpg');">Div 2 with class .cover.my-2, but without svg background is not selected</div>
    Login or Signup to reply.
  3. If you know upfront that you svg+xml is always going to be in the style attribute, you could use the [attr*=value] attribute selector. This will check if value is present in attr.

    $(document).ready(function() {
      $(".cover.my-1:contains(QQ)").css("background-color", "yellow");
      $('.cover.my-2[style*="svg+xml"]').css("background-color", "yellow");  
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <div class="cover my-1" style="background-image: url('something.jpg');">Div 1 QQ</div>
    <div class="cover my-2" style="background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc...=');">Div 2</div>
    Login or Signup to reply.
  4. This code worked for me.
    This will be turning the text color into red if it contains data:image/svg+xml and it will change the text color to black in case data:image/svg+xml is not available in the background-url link

    Here is an example: You can run the below snippet and check in this snippet
    The DIV contains SVG background image so on document load the text of the DIV
    will be in RED color

    $(document).ready(function(){
      if($('.cover').attr("style").indexOf('data:image/svg+xml') != -1){
        $('.cover').css({'color':'#ff0000'});
      }else{
        $('.cover').css({'color':'#000000'});
      }
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
    
    
    <div class="cover" style="background-image: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB2ZXJzaW9uPSIxLjEiIGlkPSJMYXllcl8xIiB4PSIwcHgiIHk9IjBweCIgdmlld0JveD0iMCAwIDEwMCAxMDAiIGVuYWJsZS1iYWNrZ3JvdW5kPSJuZXcgMCAwIDEwMCAxMDAiIHhtbDpzcGFjZT0icHJlc2VydmUiIGhlaWdodD0iMTAwcHgiIHdpZHRoPSIxMDBweCI+CjxnPgoJPHBhdGggZD0iTTI4LjE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LjgtMjQuOGMtMTMuOC05LjMtMzEuMy04LjQtNDUuOC03LjcgICBjLTkuNSwwLjUtMTcuOCwwLjktMjMuMi0xLjdjLTAuMSwwLjEtMC4yLDAuMy0wLjMsMC40Yy0xLDEuNy0yLDMuNC0yLjksNS4xQzI4LjIsNDkuNywzMy44LDUzLjksMzkuOCw1Ny42eiIvPgoJPHBhdGggZD0iTTI2LjIsODguMmMzLjMsMiw2LjcsMy42LDEwLjIsNC43Yy0zLjUtNi4yLTYuMy0xMi42LTguOC0xOC41Yy0zLjEtNy4yLTUuOC0xMy41LTktMTcuMmMtMS45LDgtMiwxNi40LTAuMywyNC43ICAgQzIwLjYsODQuMiwyMy4yLDg2LjMsMjYuMiw4OC4yeiIvPgoJPHBhdGggZD0iTTMwLjksNzNjMi45LDYuOCw2LjEsMTQuNCwxMC41LDIxLjJjMTUuNiwzLDMyLTIuMyw0Mi42LTE0LjZDNjcuNyw3Niw1Mi4yLDY5LjYsMzcuOSw2MC43QzMyLDU3LDI2LjUsNTMsMjEuMyw0OC42ICAgYy0wLjYsMS41LTEuMiwzLTEuNyw0LjZDMjQuMSw1Ny4xLDI3LjMsNjQuNSwzMC45LDczeiIvPgo8L2c+Cjwvc3ZnPg=='); width:100px; height:100px; color:#000">Div 2</div>

    Here is an example: You can run the below snippet and check in this snippet
    The DIV contains a PNG background image so on document load the text of the DIV
    will be in BLACK color

    $(document).ready(function(){
      if($('.cover').attr("style").indexOf('data:image/svg+xml') != -1){
        $('.cover').css({'color':'#ff0000'});
      }else{
        $('.cover').css({'color':'#000000'});
      }
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
    
    
    <div class="cover" style="background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAeFBMVEX///8AAABnZ2fBwcHX19eWlpby8vKSkpLp6en8/Pza2tr5+flubm6Pj4/29vbFxcV+fn5eXl4tLS2ysrJ4eHi6uro7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E8H5aTnF+jxW8mbFhNOuESHiwPrVVzgfwq2c5wMqQFAg6DSLH8jCFmuTd9yuEamEAvaou8ex43sQZdlxmb56RDWFam0e4mXoXu7X7zjsvguCr3L+gcQ9+41sJveRu6sx7+HeIaqCEhpwv/A2zXl7qn/I+IjCI0Q+lNUVK+DfOD91Zv32vYx97m5XLU9yH9hF900Hch38EIvdZjIdDjutznCcFfudx6q8RoQbkO7hohi1oVRoJ3U19KHmk3a9d01U/NGse3GzOHSgHOJLv1LVq87buOZGG5lFBaHY91aAUZz9OeZtEC6IcNGhFCOmpEUFO417T2k4elQREHyXjCu60a6EEGFtkyK5RRP04o2MLFVB3UiSFApHkfLyipH+PBa2AuYeW939OEnNr0TXLRjngnFlLlfJnYNIArwdqlDN+o2tKJHpgEkFz3XOimj0TnkK9SdAO48hdjaHapSTExg0sqAYU7aq6OzdYHnwm8ritQqqQYplPqXvgbvHfJpXRjE4oNecdSYcz8VpmFf5TgMBBu1+I2+tx0+b+cvi+OFYvh/Pu83TdP06azQfxMJj4z+fb57iLJbeqwAAAABJRU5ErkJggg=='); width:100px; height:100px; color:#000">Div 2</div>

    Hope this example will be helpful to you

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search