I'm using SVG icon in the preloader. I want to change the color of the icon's fill using a script. I have a small problem, the color changes only for a short while before closing preloader.
Here is my script:
jQuery(function($){
document.querySelector(".svgicon").getSVGDocument().getElementById("loadericon").setAttribute("fill", "#FFF");
});
And my HTML:
<object class="svgicon" type="image/svg+xml" data="'. get_template_directory_uri() . '/assets/images/preloaders/icon.svg'.'"
></object>
In the icon.svg file there is added ID "loadericon" to the HTML tag.
Can anyone help me with this?
Yes you should be able to do that with the fill attribute, like this:
As long as you don't have any CORS issues, here is a working sample:
https://raw.githack.com/heldersepu/hs-scripts/html/HTML/SVG/object.html
Now on that example I'm working from the SVG image that @enxaneta provided, hopefully that is close to yours, if not please provide your image.
One option would be using the target attribute and refer only to a part of the SVG while everything else is hidden
svg > svg:not(:target) {display: none;}
Please pay attention to the url for the data attribute:cat.svg#redcat
The SVG I'm using looks like this. Please note the CSS inside the SVG.
You can read more about this technique in the book Using SVG with CSS3 and HTML5: Vector Graphics for Web Design chapter 9: A New Point of View
UPDATE: In this case I'm using a button to change the color of the SVG paths.You may use any other event you like.