• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 428
  • Last Modified:

Help with JavaScript in SVG

We have SVGs that we are trying to write some javascript for to provide certain interactivity. Our SVGs consist of geometry that exist in several 'g' elements. Each <g> element has its own unique ID. We have been trying to write a script that will perform the following functionality in our SVGs:

1. Upon inital load, all <g> elements contain black geometry. During onmouseover, it will highlight the associated <g> element in yellow.

2. If a <g> element is clicked, then it highlights the geometry in a specified color (which can be passed in the script). This state must be maintained (i.e. onmouseover and onmouseout events will not affect the state) until the same <g> element is clicked again.

3. If the <g> is clicked a second time, it returns to its normal state (black geometry, onmouseover = yellow, onmouseout = black).

So, the cliff notes version:

state = black
if clicked = false{
onmouseover = yellow
onmouseout = black
}
if clicked = true{
onclick = color
onmouseover = null
onmouseout = null
}

We have been able to get one or the other (onmouseover/onmouseout OR onclick) to work, but we have been horribly unsuccessful in getting them to work together. Any and all help is GREATLY appreciated.
I have attached a sample subset snippet of SVG code below.
<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="EXPORT_FUNX" viewBox="-0.386222 -7.45269 13.9823 
 
8.09389" version="1.1" preserveAspectRatio="xMidYMid" xml:space="preserve" width="100%" height="100%">
<g id="XPORTMON" style="font-family:Verdana;font-size:1.4px;stroke-width:0.01px;fill:black;stroke:black;stroke-linecap:round;stroke-linejoin:round">
<text transform="matrix(0.0625 0 0 0.0625 0.918043 -4.71393)">XPORTMON</text>
    <polyline points="0.8515,-4.6821 2.2241,-4.6821 "/>
    <path d="M0.8515,-4.7271  A0.0320,0.0320 44.5493 0,0 0.8515,-4.6821 "/>
    <path d="M0.8515,-4.6371  A0.0320,0.0320 44.5493 0,0 0.8515,-4.6821 "/>
    <polyline points="2.2241,-4.6821 2.1390,-4.6821 "/>
    <polyline points="2.1390,-4.6569 2.1496,-4.6569 "/>
    <polyline points="2.1390,-4.6641 2.1709,-4.6641 "/>
    <polyline points="2.1390,-4.6713 2.1922,-4.6713 "/>
    <polyline points="2.1390,-4.6785 2.2134,-4.6785 "/>
    <polyline points="2.1390,-4.7073 2.1496,-4.7073 "/>
    <polyline points="2.1390,-4.7001 2.1709,-4.7001 "/>
    <polyline points="2.1390,-4.6929 2.1922,-4.6929 "/>
    <polyline points="2.1390,-4.6857 2.2134,-4.6857 "/>
    <polyline points="2.2241,-4.6821 2.1390,-4.7109 "/>
    <polyline points="2.1390,-4.7109 2.1390,-4.6533 "/>
    <polyline points="2.1390,-4.6533 2.2241,-4.6821 "/>
  </g>
<g id="EMSSMON" style="font-family:Verdana;font-size:1.4px;stroke-width:0.01px;fill:black;stroke:black;stroke-linecap:round;stroke-linejoin:round">
    <text transform="matrix(0.0625 0 0 0.0625 0.912091 -4.56088)">/EMSSMON</text>
    <polyline points="0.8515,-4.5246 2.2241,-4.5246 "/>
    <path d="M0.8515,-4.5696  A0.0320,0.0320 44.5493 0,0 0.8515,-4.5246 "/>
    <path d="M0.8515,-4.4796  A0.0320,0.0320 44.5493 0,0 0.8515,-4.5246 "/>
    <polyline points="2.2241,-4.5246 2.1390,-4.5246 "/>
    <polyline points="2.1390,-4.4994 2.1496,-4.4994 "/>
    <polyline points="2.1390,-4.5066 2.1709,-4.5066 "/>
    <polyline points="2.1390,-4.5138 2.1922,-4.5138 "/>
    <polyline points="2.1390,-4.5210 2.2134,-4.5210 "/>
    <polyline points="2.1390,-4.5498 2.1496,-4.5498 "/>
    <polyline points="2.1390,-4.5426 2.1709,-4.5426 "/>
    <polyline points="2.1390,-4.5354 2.1922,-4.5354 "/>
    <polyline points="2.1390,-4.5282 2.2134,-4.5282 "/>
    <polyline points="2.2241,-4.5246 2.1390,-4.5534 "/>
    <polyline points="2.1390,-4.5534 2.1390,-4.4958 "/>
    <polyline points="2.1390,-4.4958 2.2241,-4.5246 "/>
  </g>
</svg>

Open in new window

0
kmartin7
Asked:
kmartin7
  • 5
  • 3
1 Solution
 
Lolly-InkCommented:
The following modified SVG file was tested successfully using Adobe SVG Viewer in IE8 RC1.
<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="EXPORT_FUNX" 
   viewBox="-0.386222 -7.45269 13.9823 8.09389" version="1.1" preserveAspectRatio="xMidYMid" xml:space="preserve" width="100%" height="100%">
   <defs>
      <script type="text/javascript">
         <![CDATA[
            function highlight(evt, mode, colour)
            {
               var fill = evt.target.getAttribute("fill");
               var defaultColour = "yellow";
               switch (mode)
               {
                  case 0:
                     colour = fill || defaultColour;
                     break;
                  case 1:
                     colour = ((fill == defaultColour) ? "" : fill);
                     break;
                  case 2:
                     colour = ((fill == colour) ? defaultColour : colour);
                     break;
               }
               evt.target.setAttribute("fill", colour);
            }
         ]]>
      </script>
   </defs>
<g id="XPORTMON" onmouseover="highlight(evt, 0)" onmouseout="highlight(evt, 1)" onclick="highlight(evt, 2, 'green')" style="font-family:Verdana;font-size:1.4px;stroke-width:0.01px;fill:black;stroke:black;stroke-linecap:round;stroke-linejoin:round">
<text transform="matrix(0.0625 0 0 0.0625 0.918043 -4.71393)">XPORTMON</text>
    <polyline points="0.8515,-4.6821 2.2241,-4.6821 "/>
    <path d="M0.8515,-4.7271  A0.0320,0.0320 44.5493 0,0 0.8515,-4.6821 "/>
    <path d="M0.8515,-4.6371  A0.0320,0.0320 44.5493 0,0 0.8515,-4.6821 "/>
    <polyline points="2.2241,-4.6821 2.1390,-4.6821 "/>
    <polyline points="2.1390,-4.6569 2.1496,-4.6569 "/>
    <polyline points="2.1390,-4.6641 2.1709,-4.6641 "/>
    <polyline points="2.1390,-4.6713 2.1922,-4.6713 "/>
    <polyline points="2.1390,-4.6785 2.2134,-4.6785 "/>
    <polyline points="2.1390,-4.7073 2.1496,-4.7073 "/>
    <polyline points="2.1390,-4.7001 2.1709,-4.7001 "/>
    <polyline points="2.1390,-4.6929 2.1922,-4.6929 "/>
    <polyline points="2.1390,-4.6857 2.2134,-4.6857 "/>
    <polyline points="2.2241,-4.6821 2.1390,-4.7109 "/>
    <polyline points="2.1390,-4.7109 2.1390,-4.6533 "/>
    <polyline points="2.1390,-4.6533 2.2241,-4.6821 "/>
  </g>
<g id="EMSSMON" onmouseover="highlight(evt, 0)" onmouseout="highlight(evt, 1)" onclick="highlight(evt, 2, 'blue')" style="font-family:Verdana;font-size:1.4px;stroke-width:0.01px;fill:black;stroke:black;stroke-linecap:round;stroke-linejoin:round">
    <text transform="matrix(0.0625 0 0 0.0625 0.912091 -4.56088)">/EMSSMON</text>
    <polyline points="0.8515,-4.5246 2.2241,-4.5246 "/>
    <path d="M0.8515,-4.5696  A0.0320,0.0320 44.5493 0,0 0.8515,-4.5246 "/>
    <path d="M0.8515,-4.4796  A0.0320,0.0320 44.5493 0,0 0.8515,-4.5246 "/>
    <polyline points="2.2241,-4.5246 2.1390,-4.5246 "/>
    <polyline points="2.1390,-4.4994 2.1496,-4.4994 "/>
    <polyline points="2.1390,-4.5066 2.1709,-4.5066 "/>
    <polyline points="2.1390,-4.5138 2.1922,-4.5138 "/>
    <polyline points="2.1390,-4.5210 2.2134,-4.5210 "/>
    <polyline points="2.1390,-4.5498 2.1496,-4.5498 "/>
    <polyline points="2.1390,-4.5426 2.1709,-4.5426 "/>
    <polyline points="2.1390,-4.5354 2.1922,-4.5354 "/>
    <polyline points="2.1390,-4.5282 2.2134,-4.5282 "/>
    <polyline points="2.2241,-4.5246 2.1390,-4.5534 "/>
    <polyline points="2.1390,-4.5534 2.1390,-4.4958 "/>
    <polyline points="2.1390,-4.4958 2.2241,-4.5246 "/>
  </g>
</svg>

Open in new window

0
 
kmartin7Author Commented:
Thank you!
0
 
kmartin7Author Commented:
Actually, is there a way to include the stroke as well? Including fill and stroke.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Lolly-InkCommented:
Yes. Just insert the following code after line 22:
evt.target.setAttribute("stroke", colour);

Open in new window

0
 
kmartin7Author Commented:
The problem we are still having is that it doesn't affect the entire <g> element, but rather only the specific geometry. When I originally accepted the answer, it appeared to work on the small subset.

Any ideas as to how to make this work for the entire <g> element? I can ask a separate question if you prefer.

thanks!
0
 
kmartin7Author Commented:
Similar to the following, but we cannot set the clicked state to true, and get the rest of the script to recognize.

I really appreciate your assistance with this.

Cheers.
<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="EXPORT_FUNX" 
   viewBox="-0.386222 -7.45269 13.9823 8.09389" version="1.1" preserveAspectRatio="xMidYMid" xml:space="preserve" width="100%" height="100%">
   <defs>
      <script type="text/javascript">
         <![CDATA[
            function highlight(elementId, mode, color)
            {
               var element = document.getElementById(elementId);
			   var defaultcolor = "yellow";
			   var fillcolor = element.getAttribute("fill");
			   var clicked = "false";
               switch (mode)
               {
                  case 0:
				  if (clicked == "false"){
                    element.style.setProperty('fill', defaultcolor, '');				
					element.style.setProperty('stroke', defaultcolor, '');
					//alert("onmouseover: " + clicked);
					}
				  else{
					element.style.setProperty('fill', color, '');				
					element.style.setProperty('stroke', color, '');
					}			
                    break;
                  case 1:
				  if (clicked == "false") {
                    element.style.setProperty('fill', 'black', '');				
					element.style.setProperty('stroke', 'black', '');
					//alert("onmouseout: " +clicked);
					}
				  else{
					element.style.setProperty('fill', color, '');				
					element.style.setProperty('stroke', color, '');
					//alert("onmouseout: " +clicked);
					}			
                    break;
                  case 2:
				    element.style.setProperty('fill', color, '');				
					element.style.setProperty('stroke', color, '');
					clicked = "true";
					return true;
					//alert("onclick: " +clicked);
                    //break;
               }
            }
         ]]>
      </script>
   </defs>
<g id="XPORTMON" onmouseover="highlight('XPORTMON', 0, 'purple')" onmouseout="highlight('XPORTMON', 1, 'purple')" onclick="highlight('XPORTMON', 2, 'purple');" style="font-family:Verdana;font-size:1.4px;stroke-width:0.01px;fill:black;stroke:black;stroke-linecap:round;stroke-linejoin:round">
<text transform="matrix(0.0625 0 0 0.0625 0.918043 -4.71393)">XPORTMON</text>
    <polyline points="0.8515,-4.6821 2.2241,-4.6821 "/>
    <path d="M0.8515,-4.7271  A0.0320,0.0320 44.5493 0,0 0.8515,-4.6821 "/>
    <path d="M0.8515,-4.6371  A0.0320,0.0320 44.5493 0,0 0.8515,-4.6821 "/>
    <polyline points="2.2241,-4.6821 2.1390,-4.6821 "/>
    <polyline points="2.1390,-4.6569 2.1496,-4.6569 "/>
    <polyline points="2.1390,-4.6641 2.1709,-4.6641 "/>
    <polyline points="2.1390,-4.6713 2.1922,-4.6713 "/>
    <polyline points="2.1390,-4.6785 2.2134,-4.6785 "/>
    <polyline points="2.1390,-4.7073 2.1496,-4.7073 "/>
    <polyline points="2.1390,-4.7001 2.1709,-4.7001 "/>
    <polyline points="2.1390,-4.6929 2.1922,-4.6929 "/>
    <polyline points="2.1390,-4.6857 2.2134,-4.6857 "/>
    <polyline points="2.2241,-4.6821 2.1390,-4.7109 "/>
    <polyline points="2.1390,-4.7109 2.1390,-4.6533 "/>
    <polyline points="2.1390,-4.6533 2.2241,-4.6821 "/>
  </g>
<g id="EMSSMON" onmouseover="highlight('EMSSMON', 0, 'green')" onmouseout="highlight('EMSSMON', 1, 'green')" onclick="highlight('EMSSMON', 2, 'green');" style="font-family:Verdana;font-size:1.4px;stroke-width:0.01px;fill:black;stroke:black;stroke-linecap:round;stroke-linejoin:round">
    <text transform="matrix(0.0625 0 0 0.0625 0.912091 -4.56088)">/EMSSMON</text>
    <polyline points="0.8515,-4.5246 2.2241,-4.5246 "/>
    <path d="M0.8515,-4.5696  A0.0320,0.0320 44.5493 0,0 0.8515,-4.5246 "/>
    <path d="M0.8515,-4.4796  A0.0320,0.0320 44.5493 0,0 0.8515,-4.5246 "/>
    <polyline points="2.2241,-4.5246 2.1390,-4.5246 "/>
    <polyline points="2.1390,-4.4994 2.1496,-4.4994 "/>
    <polyline points="2.1390,-4.5066 2.1709,-4.5066 "/>
    <polyline points="2.1390,-4.5138 2.1922,-4.5138 "/>
    <polyline points="2.1390,-4.5210 2.2134,-4.5210 "/>
    <polyline points="2.1390,-4.5498 2.1496,-4.5498 "/>
    <polyline points="2.1390,-4.5426 2.1709,-4.5426 "/>
    <polyline points="2.1390,-4.5354 2.1922,-4.5354 "/>
    <polyline points="2.1390,-4.5282 2.2134,-4.5282 "/>
    <polyline points="2.2241,-4.5246 2.1390,-4.5534 "/>
    <polyline points="2.1390,-4.5534 2.1390,-4.4958 "/>
    <polyline points="2.1390,-4.4958 2.2241,-4.5246 "/>
  </g>
</svg>

Open in new window

0
 
Lolly-InkCommented:
Now uses evt.currentTarget (which is the g element) instead of evt.target (which was the individual element within the g(roup)).
<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="EXPORT_FUNX" 
   viewBox="-0.386222 -7.45269 13.9823 8.09389" version="1.1" preserveAspectRatio="xMidYMid" xml:space="preserve" width="100%" height="100%">
   <defs>
      <script type="text/javascript">
         <![CDATA[
            function highlight(evt, mode, colour)
            {
               var element = evt.currentTarget;
               var fill = element.getAttribute("fill");
               if (fill == "black")
               {
                  fill = "";
               }
               var defaultColour = "yellow";
               switch (mode)
               {
                  case 0:
                     colour = fill || defaultColour;
                     break;
                  case 1:
                     colour = ((fill == defaultColour) ? "" : fill);
                     break;
                  case 2:
                     colour = ((fill == colour) ? defaultColour : colour);
                     break;
               }
               element.setAttribute("fill", colour || "black");
               element.setAttribute("stroke", colour || "black");
            }
         ]]>
      </script>
   </defs>
<g id="XPORTMON" onmouseover="highlight(evt, 0)" onmouseout="highlight(evt, 1)" onclick="highlight(evt, 2, 'green')" fill="black" stroke="black" style="font-family:Verdana;font-size:1.4px;stroke-width:0.01px;stroke-linecap:round;stroke-linejoin:round">
<text transform="matrix(0.0625 0 0 0.0625 0.918043 -4.71393)">XPORTMON</text>
    <polyline points="0.8515,-4.6821 2.2241,-4.6821 "/>
    <path d="M0.8515,-4.7271  A0.0320,0.0320 44.5493 0,0 0.8515,-4.6821 "/>
    <path d="M0.8515,-4.6371  A0.0320,0.0320 44.5493 0,0 0.8515,-4.6821 "/>
    <polyline points="2.2241,-4.6821 2.1390,-4.6821 "/>
    <polyline points="2.1390,-4.6569 2.1496,-4.6569 "/>
    <polyline points="2.1390,-4.6641 2.1709,-4.6641 "/>
    <polyline points="2.1390,-4.6713 2.1922,-4.6713 "/>
    <polyline points="2.1390,-4.6785 2.2134,-4.6785 "/>
    <polyline points="2.1390,-4.7073 2.1496,-4.7073 "/>
    <polyline points="2.1390,-4.7001 2.1709,-4.7001 "/>
    <polyline points="2.1390,-4.6929 2.1922,-4.6929 "/>
    <polyline points="2.1390,-4.6857 2.2134,-4.6857 "/>
    <polyline points="2.2241,-4.6821 2.1390,-4.7109 "/>
    <polyline points="2.1390,-4.7109 2.1390,-4.6533 "/>
    <polyline points="2.1390,-4.6533 2.2241,-4.6821 "/>
  </g>
<g id="EMSSMON" onmouseover="highlight(evt, 0)" onmouseout="highlight(evt, 1)" onclick="highlight(evt, 2, 'blue')" fill="black" stroke="black" style="font-family:Verdana;font-size:1.4px;stroke-width:0.01px;stroke-linecap:round;stroke-linejoin:round">
    <text transform="matrix(0.0625 0 0 0.0625 0.912091 -4.56088)">/EMSSMON</text>
    <polyline points="0.8515,-4.5246 2.2241,-4.5246 "/>
    <path d="M0.8515,-4.5696  A0.0320,0.0320 44.5493 0,0 0.8515,-4.5246 "/>
    <path d="M0.8515,-4.4796  A0.0320,0.0320 44.5493 0,0 0.8515,-4.5246 "/>
    <polyline points="2.2241,-4.5246 2.1390,-4.5246 "/>
    <polyline points="2.1390,-4.4994 2.1496,-4.4994 "/>
    <polyline points="2.1390,-4.5066 2.1709,-4.5066 "/>
    <polyline points="2.1390,-4.5138 2.1922,-4.5138 "/>
    <polyline points="2.1390,-4.5210 2.2134,-4.5210 "/>
    <polyline points="2.1390,-4.5498 2.1496,-4.5498 "/>
    <polyline points="2.1390,-4.5426 2.1709,-4.5426 "/>
    <polyline points="2.1390,-4.5354 2.1922,-4.5354 "/>
    <polyline points="2.1390,-4.5282 2.2134,-4.5282 "/>
    <polyline points="2.2241,-4.5246 2.1390,-4.5534 "/>
    <polyline points="2.1390,-4.5534 2.1390,-4.4958 "/>
    <polyline points="2.1390,-4.4958 2.2241,-4.5246 "/>
  </g>
</svg>

Open in new window

0
 
kmartin7Author Commented:
Excellent. Thank you for your help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now