Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with JavaScript in SVG

Posted on 2009-03-30
8
Medium Priority
?
420 Views
Last Modified: 2012-05-06
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
Comment
Question by:kmartin7
  • 5
  • 3
8 Comments
 
LVL 16

Accepted Solution

by:
Lolly-Ink earned 2000 total points
ID: 24024118
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
 
LVL 11

Author Closing Comment

by:kmartin7
ID: 31564335
Thank you!
0
 
LVL 11

Author Comment

by:kmartin7
ID: 24029846
Actually, is there a way to include the stroke as well? Including fill and stroke.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

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

Open in new window

0
 
LVL 11

Author Comment

by:kmartin7
ID: 24051556
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
 
LVL 11

Author Comment

by:kmartin7
ID: 24053708
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
 
LVL 16

Expert Comment

by:Lolly-Ink
ID: 24055129
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
 
LVL 11

Author Comment

by:kmartin7
ID: 24055505
Excellent. Thank you for your help!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question