Answers for "svg jquery onclick color change in wordpress"

0

svg jquery onclick color change in wordpress

<script type="text/javascript">
    $(document).ready(function() 
    {

        //Redo XML
        jQuery('img.svg').each(function(){
            var $img = jQuery(this);
            var imgID = $img.attr('id');
            var imgClass = $img.attr('class');
            var imgURL = $img.attr('src');

            jQuery.get(imgURL, function(data) {
                // Get the SVG tag, ignore the rest
                var $svg = jQuery(data).find('svg');

                // Add replaced image's ID to the new SVG
                if(typeof imgID !== 'undefined') {
                    $svg = $svg.attr('id', imgID);
                }
                // Add replaced image's classes to the new SVG
                if(typeof imgClass !== 'undefined') {
                    $svg = $svg.attr('class', imgClass+' replaced-svg');
                }

                // Remove any invalid XML tags as per http://validator.w3.org
                $svg = $svg.removeAttr('xmlns:a');

                // Replace image with new SVG
                $img.replaceWith($svg);

            }, 'xml');

        });

    });

    function changeColor(color)
    {

        //obj Father
        $("#imgSvg").css("fill", color);
        //objs children
        $('#imgSvg').children().css('fill', color);
    }
    </script>




    <img id="imgSvg" xmlns="http://www.w3.org/2000/svg" version="1.1" class="svg" src="imgTest.svg"/>

    <br />
    <br />

    <button onclick="changeColor('#C0C0C0')">Gray</button>
    <button onclick="changeColor('#FFF')">White</button>
    <button onclick="changeColor('#000')">Black</button>
Posted by: Guest on July-25-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language