SVG tooltip disappears immediately - javascript
I've got a map with several regions and want to show a tooltip with its name when hovering over it.
The tooltip will display but disappears immediately. It doesn't matter where I put the tooltip (g, a or polygon), but I believe the g-node is the right place.
Example with three circles:
https://codepen.io/suntrop/pen/BmLZzN
<svg width="246px" height="64px" viewBox="0 0 246 64" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- Generator: Sketch 47.1 (45422) - http://www.bohemiancoding.com/sketch -->
<desc>Created with Sketch.</desc>
<defs></defs>
<g class="testtooltip" title="My Tooltip" id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<circle id="Oval" fill="#D8D8D8" cx="32" cy="32" r="32"></circle>
</g>
<g class="testtooltip" title="Will Show" id="Page-2" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<circle id="Oval" fill="#B8E986" cx="123" cy="32" r="32"></circle>
</g>
<g class="testtooltip" title="And Hide" id="Page-3" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<circle id="Oval" fill="#D8D8D8" cx="214" cy="32" r="32"></circle>
</g>
</svg>
The JS
$(function() {
UIkit.tooltip('.testtooltip').show();
});
When I use the tooltip on a P or IMG element the tooltip stays while hovering the element.
Looks like UIkit doesn't support tooltips on SVG elements. To decide whether an open tooltip should remain open, UIkit regularly calls an isVisible() function which in turn reads the target element's offsetHeight. SVG elements don't have an offsetHeight: https://www.chromestatus.com/features/5724912467574784
Thus, UIkit thinks the element is hidden and immediately hides the tooltip too.
Related
SVG Mask Path is cropped
I am trying to build a javascript experiment of a dynamic chain which follows the mouse cursor. Therefore I am useing a SVG wwith the following path: <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 240"> <defs> <path id="a" d="M143 158q-31-139 9-99" fill="none" stroke-linecap="round"/> <mask id="b"> <rect x="0%" y="0%" width="100%" height="100%" fill="#fff"/> <use href="#a" stroke-width="4" stroke-dasharray="6 14" stroke-dashoffset="7" stroke="#000"/> </mask> </defs> <use href="#a" stroke-width="8" stroke-dasharray="6 14" stroke-dashoffset="7" stroke="#333" stroke-opacity=".8" mask="url(#b)"/> <use href="#a" stroke-width="2" stroke-dasharray="12 8" stroke="#333" stroke-opacity=".8"/> </svg> Unfortunately the chain is cropped: Why is this happening? You can see the full experiment here (desktop only).
Long story short: A mask works within an object's bounding box, but the bounding box of any element doesn't include the stroke width. Therefore, the default mask adds 10% padding around the bounding box with its x, y, width, height and maskUnits attributes. This works in most cases, but fails when an element is slim and almost horizontal or vertical. See the below image: The blue rectangle is your path's bounding box, and the green is the area where the mask does its job. You can see that some of the path sticks out to the left and right. So you must change the mask attributes to work for you. For example, make it cover the whole image: <mask id="b" maskUnits="userSpaceOnUse" x="0" y="0" width="100%" height="100%">
I fixed it by adding maskUnits="userSpaceOnUse" https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/maskUnits (changed the viewBox to better display it in an SO snippet) <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 40 300 240"> <defs> <path id="a" d="M143 158q-31-139 9-99" fill="none" stroke-linecap="round"/> <mask id="b" maskUnits="userSpaceOnUse"> <rect x="0%" y="0%" width="100%" height="100%" fill="#fff"/> <use href="#a" stroke-width="4" stroke-dasharray="6 14" stroke-dashoffset="7" stroke="#000"/> </mask> </defs> <use href="#a" stroke-width="8" stroke-dasharray="6 14" stroke-dashoffset="7" stroke="#333" stroke-opacity=".8" mask="url(#b)"/> <use href="#a" stroke-width="2" stroke-dasharray="12 8" stroke="#333" stroke-opacity=".8"/> </svg>
How can I make a toggle to toggle the visibility of an inline SVG?
I'm making a website where you can create your own character in order to learn more about SVGs. I want the user to be able to select different poses. I was wondering how I could toggle the visibility of an SVG on the website to make one disappear and another appear. Here is my SVG code: <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 720 720" style="enable-background:new 0 0 720 720;" xml:space="preserve"> <g id="skin"> <circle class="skin" cx="364.42" cy="383" r="278" /> </g> <g id="mouth"> <path class="mouth" d="M172.92,383c127.67,0,255.33,0,383,0c0,105.05-86.45,191.5-191.5,191.5S172.92,488.05,172.92,383z"/> </g> <g id="hair"> <path class = "hair" d="M107.4,276.86c-2.76-50.59,10.76-81.24,24.2-100.13C189.57,95.31,331.92,112,341.92,57.91 c2.04-11.03-2.07-21.46-6.93-29.61c18.47-6.31,81.92-25.39,151.28,3.28c94.59,39.09,121.88,137.21,127.56,160.84 c7.93,32.98,7.18,61.32,5.46,79.42c-38.79-64.73-78.17-85.57-107.42-92.42c-43.32-10.15-60.72,11.26-139.64,11.71 c-67.77,0.39-78.13-15.27-119.49-10.14C216.75,185.45,165.19,204.69,107.4,276.86z"/> </g> <g id="eyes"> <g> <circle class = "eyes" cx="251.17" cy="271.25" r="52.4"/> <circle class = "eyes" cx="477.67" cy="271.25" r="52.4"/> </g> </g> <g id="clothing"> <rect x="236.92" y="644.4" width="255" height="180"/> </g> <g id="tie"> <rect class="tie" x="341.82" y="653.68" transform="matrix(0.7071 0.7071 -0.7071 0.7071 584.9371 -59.6038)" class="st0" width="45.2" height="45.2"/> <path class="tie" d="M364.95,927.93c-6.29-12.71-12.59-25.42-18.88-38.13c3.44-63.19,6.89-126.39,10.33-189.58 c5.1,0.25,10.2,0.49,15.29,0.74c4.13,62.75,8.25,125.49,12.38,188.24C377.7,902.11,371.32,915.02,364.95,927.93z"/> </g> </svg> The SVG is just a sample right now before I make the actual assets.
My SVG elements are not clickable anymore with Firefox Quantum 67.0
I have an SVG file. There are elements that can be clicked and can call functions from a JavaScript file when clicked. It works perfectly with Google Chrome, IE and earlier versions of Firefox. But I cannot make it work with Firefox 67 or later. I have already tried to change my onmousedown function to onclick. I have found a website to view my SVG file. It also works fine. Here is some code: <?xml version="1.0" encoding="utf-8"?> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="106.892mm" viewBox="0 0 1370.4 484.8" width="302.154mm"> <g fill="none" fill-rule="evenodd" stroke="black" stroke-linecap="square" stroke-linejoin="bevel" stroke-width="1"> <g clip-path="url(#clip464)" cursor="pointer" fill="green" fill-opacity="1" onmousedown="parent.OpenPane('mGraph');" opacity="1" stroke="none" stroke-opacity="0" transform="matrix(1,0,0,1,392,262)"> <path d="M0,0 L30,0 L30,32 L0,32 L0,0 z" fill-rule="evenodd" vector-effect="none"/> </g> </g> </svg> edit1: you can find a spesific code script on this site -> JSFiddle link! It works with Google Chrome as expected, but not with Firefox v-69.
You have a clip-path that does not exist: clip-path="url(#clip464)" There is no element with id clip464 in your example. This is a known bug but you can work around it easily by removing the useless attribute.
I'm not sure you can reference any JS outside the SVG. I tried your code on Chrome. Including all JS code inside the SVG works as expected. Alternatively, you can also attach event listeners from outside the SVG. Check out the code below: All JS inside the SVG <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="106.892mm" viewBox="0 0 1370.4 484.8" width="302.154mm"> <script type="text/ecmascript"><![CDATA[ function OpenPane(str) { alert(str); } ]]> </script> <g fill="none" fill-rule="evenodd" stroke="black" stroke-linecap="square" stroke-linejoin="bevel" stroke-width="1"> <g clip-path="url(#clip464)" cursor="pointer" fill="green" fill-opacity="1" onmousedown="OpenPane('mGraph');" opacity="1" stroke="none" stroke-opacity="0" transform="matrix(1,0,0,1,392,262)"> <path d="M0,0 L30,0 L30,32 L0,32 L0,0 z" fill-rule="evenodd" vector-effect="none"/> </g> </g> </svg> JS outside SVG <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="106.892mm" viewBox="0 0 1370.4 484.8" width="302.154mm"> <g fill="none" fill-rule="evenodd" stroke="black" stroke-linecap="square" stroke-linejoin="bevel" stroke-width="1"> <g clip-path="url(#clip464)" cursor="pointer" fill="green" fill-opacity="1" id="sample-id" opacity="1" stroke="none" stroke-opacity="0" transform="matrix(1,0,0,1,392,262)"> <path d="M0,0 L30,0 L30,32 L0,32 L0,0 z" fill-rule="evenodd" vector-effect="none"/> </g> </g> </svg> <script type="text/ecmascript"> const el = document.getElementById('sample-id'); el.addEventListener('click', () => { document.getElementById('sample-id').setAttribute('fill', 'red'); }); el.addEventListener('mouseleave', () => { document.getElementById('sample-id').setAttribute('fill', 'green'); }); </script> JSFiddle
move SVG group on path trail based on percentage of whole path trail
I am trying to find a way to move an group (id="avatar") inside an SVG with jQuery based on an percentage of the whole trail of an path element (id="path" > path), inside the SVG. So set the avatar with transform->translate to the right position dynamically for example 20% of the trail. So te input is 20, then it should be set on 20% of the trail of #path > path. I can't find any sort of function or parsing mechanism to archive this goal. Any suggestions? The SVG is used in the browser as an whole SVG html5 element with all their nested SVG tags. So not loade like in an img->src. SVG image: SVG code: <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1000 600" xml:space="preserve"> <style type="text/css"> .st5{fill:#43A592;stroke:#8C6239;stroke-width:0.5;stroke-miterlimit:10;} .st6{fill:none;stroke:#603813;stroke-width:8;stroke-linecap:square;stroke-miterlimit:10;} .st7{fill:none;stroke:#603813;stroke-width:8;stroke-linecap:square;stroke-miterlimit:10;stroke-dasharray:5.0414,12.0993;} .st8{fill:#405813;} .st9{fill:#7FBFD6;} </style> <g id="path"> <path class="st7" d="M253.83,291.92C438.6,278.28,338.5,457,422.2,429.99c87.72-28.31-71.17-110.68,22.64-190.57 c54.07-46.05,161.79-22.03,138.18,79.89c-13.69,59.11-53.79,87.21-30.99,132.4c0,0,20.26,23.49,61.35,18.96"/> </g> <g id="avatar" transform="translate(10,20)"> <g> <g> <circle class="st9" cx="276.22" cy="290.06" r="26.83"/> <path class="st8" d="M276.22,317.69c-15.23,0-27.62-12.39-27.62-27.62s12.39-27.62,27.62-27.62s27.62,12.39,27.62,27.62 S291.45,317.69,276.22,317.69z M276.22,264.04c-14.35,0-26.03,11.68-26.03,26.03s11.68,26.03,26.03,26.03 s26.03-11.68,26.03-26.03S290.57,264.04,276.22,264.04z"/> </g> <g> <g> <path class="st16" d="M264.95,293.51c-0.35-0.11-0.74,0.03-0.93,0.35l-4.62,7.56c-0.35,0.58,0.02,1.33,0.7,1.39 c3.16,0.29,11.31,1.43,23.76,5.78c0.44,0.15,0.86-0.3,0.65-0.73C283.15,305.1,278.44,297.84,264.95,293.51z"/> </g> <g> <path class="st16" d="M284.68,279.17c-2.01-6.83-8.8-10.85-15.17-8.97c-0.04,0.01-0.09,0.03-0.13,0.04 c-0.06-0.01-0.13-0.01-0.19-0.02c-0.07,0.05-0.13,0.09-0.19,0.14c-6.04,2.09-9.34,8.94-7.39,15.59c0,0-0.76,2.95-2.22,5.82 c0,0,6.79-0.4,8.31-7.6c0,0,0.02,0.05,0,0l9.33-2.75c0.01,0.03,0,0,0,0c8.13,6.32,13.75,1.44,13.75,1.44 S287.35,280.89,284.68,279.17z"/> </g> <g> <path class="st8" d="M296.91,298.57c-0.04-0.3-0.29-0.52-0.59-0.52h-3.56c-0.33,0-0.6,0.27-0.6,0.6c0,1.34-0.33,1.94-0.6,2.2 c-0.18,0.18-0.4,0.27-0.65,0.26c-0.47-0.01-1.14-0.76-1.62-1.83c-1.25-2.77-3.05-3.78-4.49-4.12v-1.83 c0-0.07,0.06-0.12,0.12-0.12h1.72c0.09,0,0.19-0.02,0.27-0.06c0.25-0.12,0.37-0.39,0.33-0.64c-0.46-2.69-1.3-5.53-1.85-7.27 c3.56-0.05,5.75-1.89,5.78-1.92c0.15-0.13,0.22-0.32,0.2-0.51c-0.02-0.19-0.13-0.36-0.3-0.46c-0.03-0.02-3.07-1.76-5.62-3.38 c-0.17-0.11-0.31-0.28-0.37-0.47c-2.19-6.54-8.67-10.46-14.95-9.07l0.35-0.89c0.12-0.31-0.04-0.67-0.36-0.78 c-0.31-0.11-0.65,0.07-0.77,0.37l-0.34,0.87c-0.05,0.14-0.23,0.19-0.35,0.1l-0.69-0.52c-0.27-0.2-0.65-0.14-0.85,0.13 c-0.19,0.27-0.11,0.64,0.15,0.84l0.74,0.55c-5.7,2.43-8.79,9.05-7.12,15.55c0.05,0.2,0.04,0.4-0.03,0.59 c-0.34,0.99-1.18,3.34-2.03,5.27c-0.07,0.15-0.08,0.32-0.02,0.47c0.09,0.24,0.32,0.38,0.56,0.38c0.01,0,0.03,0,0.04,0 c0.04,0,2.17-0.17,4.39-1.48c0.22,0.56,0.48,1.26,0.69,2.01c-0.4,0.06-0.78,0.29-1,0.66c0,0,0,0,0,0l-4.62,7.56 c-0.28,0.45-0.3,1-0.06,1.47c0.24,0.47,0.69,0.78,1.21,0.83c2.93,0.27,11.11,1.37,23.62,5.75c0.12,0.04,0.25,0.06,0.37,0.06 c0.32,0,0.62-0.13,0.84-0.38c0.3-0.34,0.37-0.83,0.17-1.24c-0.72-1.48-2.29-4.05-5.38-6.8c0.84,0.21,1.68,0.35,2.49,0.42 c0.07,0.01,0.14,0.01,0.2,0.01c0.64,0,1.26-0.23,1.72-0.66c0.47-0.43,0.72-1.02,0.72-1.67v-2.29c2.23,0.86,2.77,2.18,3.39,3.7 c0.38,0.93,0.76,1.89,1.54,2.82c0.8,0.96,1.75,1.43,2.91,1.43c0.27,0,0.56-0.03,0.85-0.08 C296.75,303.92,297.15,300.37,296.91,298.57z M285.22,280.22c1.65,1.04,3.43,2.09,4.44,2.68c-1.61,0.93-5.77,2.54-11.3-1.25 l5.51-1.62C284.32,279.9,284.81,279.97,285.22,280.22z M272.69,270.34c4.78,0,9.32,3.21,11.14,8.16 c0.05,0.15-0.03,0.3-0.18,0.35l-5.99,1.76c-0.1,0.03-0.21-0.03-0.24-0.14c-0.25-1-0.74-2.77-1.58-4.6 c-1.2-2.63-2.68-4.48-4.37-5.47C271.89,270.36,272.29,270.34,272.69,270.34z M269.12,271.08c0.15-0.15,0.37-0.21,0.57-0.15 c4.12,1.11,6.06,7.7,6.59,9.9c0.02,0.1-0.03,0.2-0.13,0.23l-7.73,2.27c-0.1,0.03-0.2-0.02-0.23-0.12 C267.48,281.06,265.68,274.45,269.12,271.08z M266.97,272c-0.94,1.77-1.27,4.06-0.98,6.79c0.22,2.01,0.72,3.77,1.04,4.74 c0.03,0.1-0.02,0.21-0.13,0.24l-4.61,1.35c-0.15,0.04-0.3-0.05-0.34-0.2C260.81,279.76,262.91,274.58,266.97,272z M261.95,286.82c0.08-0.24,0.28-0.42,0.52-0.49l4.57-1.34c-1.35,4.32-4.91,5.64-6.67,6.03 C261.03,289.4,261.63,287.72,261.95,286.82z M283.89,307.97c-12.54-4.37-20.77-5.48-23.74-5.75c-0.16-0.01-0.23-0.13-0.25-0.17 c-0.02-0.05-0.07-0.18,0.01-0.31c0,0,4.62-7.56,4.62-7.56c0.05-0.08,0.15-0.12,0.24-0.09c4.49,1.44,8.93,3.51,12.67,6.42 C280.02,302.53,282.4,305.02,283.89,307.97z M283.61,292.61v6.31c0,0.39-0.19,0.64-0.34,0.79c-0.26,0.24-0.63,0.36-1.02,0.33 c-1.17-0.09-2.44-0.37-3.64-0.79c-1.06-0.37-2.07-0.89-3.02-1.5c-1.62-1.04-3.49-2.05-5.65-2.99l0.5-2.38 c0.07-0.31-0.13-0.6-0.44-0.7c-1.28-0.42-1.67-1.57-1.78-2.09c-0.27-1.19,0.05-2.43,0.76-3.03c1.42-1.2,2.76-0.33,2.89-0.25 c0.24,0.15,0.43,0.18,0.67,0.05c0.11-0.06,0.19-0.19,0.22-0.29c0.29-0.78,0.7-2.03,1.06-3.08l3.08-0.91 c2.69,2.03,5.14,2.88,7.21,3.1c0.49,1.49,1.29,4.12,1.8,6.69c0.02,0.08-0.04,0.15-0.12,0.15h-1.58 C283.88,292.01,283.61,292.28,283.61,292.61z"/> </g> </g> </g> </g> </svg>
In order to get the position of a point on a path you need to use path.getPointAtLength. Here is what I've done: I've moved the avatar in a <defs> element so that I can use it latter. Also I've translated everything in the avatar in the origin of the svg canvas. The <use> elements can get an x and a y attributes in order to be able to position the used element on the svg canvas. In Javascript I'm calculating the x and y for the avatar using the path.getPointAtLength method. In this demo an input type range changes the position of the avatar on the path; In your code you have a transform attribute for the avatar. I'm using this transform for the use element. Also: for the demo I've changed the value of the viewBox attribute of your svg element. You may change it back at what you want. let pathlength = path.getTotalLength(); let pos = path.getPointAtLength(0); theUse.setAttributeNS(null,"x", pos.x); theUse.setAttributeNS(null,"y", pos.y); theRange.addEventListener("input", ()=>{ let perc = parseInt(theRange.value); let leng = pathlength * perc/100; pos = path.getPointAtLength(leng); theUse.setAttributeNS(null,"x", pos.x); theUse.setAttributeNS(null,"y", pos.y); }) svg{border:1px solid;overflow:visible; width:95vh; display:block; margin:1em auto;} <input type="range" id="theRange" value="0"/> <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="230 200 400 300" xml:space="preserve"> <style type="text/css"> .st5{fill:#43A592;stroke:#8C6239;stroke-width:0.5;stroke-miterlimit:10;} .st6{fill:none;stroke:#603813;stroke-width:8;stroke-linecap:square;stroke-miterlimit:10;} .st7{fill:none;stroke:#603813;stroke-width:8;stroke-linecap:square;stroke-miterlimit:10;stroke-dasharray:5.0414,12.0993;} .st8{fill:#405813;} .st9{fill:#7FBFD6;} </style> <defs> <g id="avatar" transform="translate(-276.22, -290.06)"> <g> <g> <circle class="st9" cx="276.22" cy="290.06" r="26.83"/> <path class="st8" d="M276.22,317.69c-15.23,0-27.62-12.39-27.62-27.62s12.39-27.62,27.62-27.62s27.62,12.39,27.62,27.62 S291.45,317.69,276.22,317.69z M276.22,264.04c-14.35,0-26.03,11.68-26.03,26.03s11.68,26.03,26.03,26.03 s26.03-11.68,26.03-26.03S290.57,264.04,276.22,264.04z"/> </g> <g> <g> <path class="st16" d="M264.95,293.51c-0.35-0.11-0.74,0.03-0.93,0.35l-4.62,7.56c-0.35,0.58,0.02,1.33,0.7,1.39 c3.16,0.29,11.31,1.43,23.76,5.78c0.44,0.15,0.86-0.3,0.65-0.73C283.15,305.1,278.44,297.84,264.95,293.51z"/> </g> <g> <path class="st16" d="M284.68,279.17c-2.01-6.83-8.8-10.85-15.17-8.97c-0.04,0.01-0.09,0.03-0.13,0.04 c-0.06-0.01-0.13-0.01-0.19-0.02c-0.07,0.05-0.13,0.09-0.19,0.14c-6.04,2.09-9.34,8.94-7.39,15.59c0,0-0.76,2.95-2.22,5.82 c0,0,6.79-0.4,8.31-7.6c0,0,0.02,0.05,0,0l9.33-2.75c0.01,0.03,0,0,0,0c8.13,6.32,13.75,1.44,13.75,1.44 S287.35,280.89,284.68,279.17z"/> </g> <g> <path class="st8" d="M296.91,298.57c-0.04-0.3-0.29-0.52-0.59-0.52h-3.56c-0.33,0-0.6,0.27-0.6,0.6c0,1.34-0.33,1.94-0.6,2.2 c-0.18,0.18-0.4,0.27-0.65,0.26c-0.47-0.01-1.14-0.76-1.62-1.83c-1.25-2.77-3.05-3.78-4.49-4.12v-1.83 c0-0.07,0.06-0.12,0.12-0.12h1.72c0.09,0,0.19-0.02,0.27-0.06c0.25-0.12,0.37-0.39,0.33-0.64c-0.46-2.69-1.3-5.53-1.85-7.27 c3.56-0.05,5.75-1.89,5.78-1.92c0.15-0.13,0.22-0.32,0.2-0.51c-0.02-0.19-0.13-0.36-0.3-0.46c-0.03-0.02-3.07-1.76-5.62-3.38 c-0.17-0.11-0.31-0.28-0.37-0.47c-2.19-6.54-8.67-10.46-14.95-9.07l0.35-0.89c0.12-0.31-0.04-0.67-0.36-0.78 c-0.31-0.11-0.65,0.07-0.77,0.37l-0.34,0.87c-0.05,0.14-0.23,0.19-0.35,0.1l-0.69-0.52c-0.27-0.2-0.65-0.14-0.85,0.13 c-0.19,0.27-0.11,0.64,0.15,0.84l0.74,0.55c-5.7,2.43-8.79,9.05-7.12,15.55c0.05,0.2,0.04,0.4-0.03,0.59 c-0.34,0.99-1.18,3.34-2.03,5.27c-0.07,0.15-0.08,0.32-0.02,0.47c0.09,0.24,0.32,0.38,0.56,0.38c0.01,0,0.03,0,0.04,0 c0.04,0,2.17-0.17,4.39-1.48c0.22,0.56,0.48,1.26,0.69,2.01c-0.4,0.06-0.78,0.29-1,0.66c0,0,0,0,0,0l-4.62,7.56 c-0.28,0.45-0.3,1-0.06,1.47c0.24,0.47,0.69,0.78,1.21,0.83c2.93,0.27,11.11,1.37,23.62,5.75c0.12,0.04,0.25,0.06,0.37,0.06 c0.32,0,0.62-0.13,0.84-0.38c0.3-0.34,0.37-0.83,0.17-1.24c-0.72-1.48-2.29-4.05-5.38-6.8c0.84,0.21,1.68,0.35,2.49,0.42 c0.07,0.01,0.14,0.01,0.2,0.01c0.64,0,1.26-0.23,1.72-0.66c0.47-0.43,0.72-1.02,0.72-1.67v-2.29c2.23,0.86,2.77,2.18,3.39,3.7 c0.38,0.93,0.76,1.89,1.54,2.82c0.8,0.96,1.75,1.43,2.91,1.43c0.27,0,0.56-0.03,0.85-0.08 C296.75,303.92,297.15,300.37,296.91,298.57z M285.22,280.22c1.65,1.04,3.43,2.09,4.44,2.68c-1.61,0.93-5.77,2.54-11.3-1.25 l5.51-1.62C284.32,279.9,284.81,279.97,285.22,280.22z M272.69,270.34c4.78,0,9.32,3.21,11.14,8.16 c0.05,0.15-0.03,0.3-0.18,0.35l-5.99,1.76c-0.1,0.03-0.21-0.03-0.24-0.14c-0.25-1-0.74-2.77-1.58-4.6 c-1.2-2.63-2.68-4.48-4.37-5.47C271.89,270.36,272.29,270.34,272.69,270.34z M269.12,271.08c0.15-0.15,0.37-0.21,0.57-0.15 c4.12,1.11,6.06,7.7,6.59,9.9c0.02,0.1-0.03,0.2-0.13,0.23l-7.73,2.27c-0.1,0.03-0.2-0.02-0.23-0.12 C267.48,281.06,265.68,274.45,269.12,271.08z M266.97,272c-0.94,1.77-1.27,4.06-0.98,6.79c0.22,2.01,0.72,3.77,1.04,4.74 c0.03,0.1-0.02,0.21-0.13,0.24l-4.61,1.35c-0.15,0.04-0.3-0.05-0.34-0.2C260.81,279.76,262.91,274.58,266.97,272z M261.95,286.82c0.08-0.24,0.28-0.42,0.52-0.49l4.57-1.34c-1.35,4.32-4.91,5.64-6.67,6.03 C261.03,289.4,261.63,287.72,261.95,286.82z M283.89,307.97c-12.54-4.37-20.77-5.48-23.74-5.75c-0.16-0.01-0.23-0.13-0.25-0.17 c-0.02-0.05-0.07-0.18,0.01-0.31c0,0,4.62-7.56,4.62-7.56c0.05-0.08,0.15-0.12,0.24-0.09c4.49,1.44,8.93,3.51,12.67,6.42 C280.02,302.53,282.4,305.02,283.89,307.97z M283.61,292.61v6.31c0,0.39-0.19,0.64-0.34,0.79c-0.26,0.24-0.63,0.36-1.02,0.33 c-1.17-0.09-2.44-0.37-3.64-0.79c-1.06-0.37-2.07-0.89-3.02-1.5c-1.62-1.04-3.49-2.05-5.65-2.99l0.5-2.38 c0.07-0.31-0.13-0.6-0.44-0.7c-1.28-0.42-1.67-1.57-1.78-2.09c-0.27-1.19,0.05-2.43,0.76-3.03c1.42-1.2,2.76-0.33,2.89-0.25 c0.24,0.15,0.43,0.18,0.67,0.05c0.11-0.06,0.19-0.19,0.22-0.29c0.29-0.78,0.7-2.03,1.06-3.08l3.08-0.91 c2.69,2.03,5.14,2.88,7.21,3.1c0.49,1.49,1.29,4.12,1.8,6.69c0.02,0.08-0.04,0.15-0.12,0.15h-1.58 C283.88,292.01,283.61,292.28,283.61,292.61z"/> </g> </g> </g> </g> </defs> <g> <path class="st7" id="path" d="M253.83,291.92C438.6,278.28,338.5,457,422.2,429.99c87.72-28.31-71.17-110.68,22.64-190.57 c54.07-46.05,161.79-22.03,138.18,79.89c-13.69,59.11-53.79,87.21-30.99,132.4c0,0,20.26,23.49,61.35,18.96"/> </g> <use id="theUse" transform="translate(0,20)" xlink:href="#avatar" /> </svg>
CSS style addition has stopped javascript function from working
I created this javascript function to change the colour of guitar string in an SVG. Originally, the SVG had 'stroke' colours defined in the markup and at that point the function worked, so that when I pressed the button, the colour of the string 'e-low' changed. However, I decided I wanted to add CSS default style to the stroke colour (which you can see at the stop of the code) because I intend to have functionality so that when the button is pressed a second time, the colour returns to the default colour defined in the style section. Since I've added this, and changed the colour in the SVG to 'None', the javascript function has stopped working and the colour doesn't change whatsoever, and I don't know why. Before I added the css style element function svgMod() { var s = document.getElementById("e-low"); s.setAttribute("stroke", "#000000"); } #e-string, #b-string, #g-string, #d-string, #a-string, #e-low { stroke: #adad8b; } <svg xmlns="http://www.w3.org/2000/svg" ......... <path id="e-string" stroke="none" fill="none" stroke-width="2" d="M502.583,13.046v411.966" /> <path id="b-string" stroke="none" fill="none" stroke-width="2.5" d="M472.366,13.046v411.966" /> <path id="g-string" stroke="none" fill="none" stroke-width="3" d="M440.134,13.046v411.966" /> <path id="d-string" stroke="none" fill="none" stroke-width="3.3" d="M405.887,13.046v411.966" /> <path id="a-string" stroke="none" fill="none" stroke-width="3.5" d="M373.655,13.042v411.965" /> <path id="e-low" stroke="none" fill="none" stroke-width="4" d="M341.423,13.046v411.966" /> </svg> <button class="btn" onclick="svgMod(); return false;">Test 1</button>
Make sure you have the right viewBox and a proper sizing for the SVG element, I just added a random viewBox to see the guitar strings. You can read more about viewBox in this link The viewBox attribute allows you to specify that a given set of graphics stretch to fit a particular container element. Also as #CBroe mentioned using s.style.stroke = '#000000' fits better to modify the styles of a element. function svgMod() { var s = document.getElementById("e-low"); s.style.stroke = "#000000"; } #e-string, #b-string, #g-string, #d-string, #a-string, #e-low { stroke: #adad8b; } <svg xmlns="http://www.w3.org/2000/svg" width="210" viewBox="0 0 600 600"> <path id="e-string" stroke="none" fill="none" stroke-width="2" d="M502.583,13.046v411.966"/> <path id="b-string" stroke="none" fill="none" stroke-width="2.5" d="M472.366,13.046v411.966"/> <path id="g-string" stroke="none" fill="none" stroke-width="3" d="M440.134,13.046v411.966"/> <path id="d-string" stroke="none" fill="none" stroke-width="3.3" d="M405.887,13.046v411.966"/> <path id="a-string" stroke="none" fill="none" stroke-width="3.5" d="M373.655,13.042v411.965"/> <path id="e-low" stroke="none" fill="none" stroke-width="4" d="M341.423,13.046v411.966"/> </svg> <button class="btn" onclick="svgMod(); return false;">Test 1</button>