Adding an SVG Element via ECMAScript : JavaScript « SVG « XML

Home
XML
1.CSS Style
2.SVG
3.XML Schema
4.XQuery
5.XSLT stylesheet
XML » SVG » JavaScript 




Adding an SVG Element via ECMAScript

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
 "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">

<svg width="100%" height="100%"
     xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink">
  <script type="text/ecmascript">
    var rectNode = null;
    var parent   = null;
    var svgDocument = null;

    function remove(event)
    {
       rectNode = event.target;
       parent   = rectNode.parentNode;
       parent.removeChild(rectNode);

       svgDocument = parent.getOwnerDocument();

       circleNode = svgDocument.createElement("circle");

       circleNode.setAttribute("style","fill:blue");
       circleNode.setAttribute("cx""100");
       circleNode.setAttribute("cy""100");
       circleNode.setAttribute("r",  "100");

       parent.appendChild(circleNode);
    }

  </script>

  <g transform="translate(50,50)">
     <text id="text1" font-size="24" fill="blue">
       Click inside the rectangle:
     </text>
  </g>

  <g transform="translate(50,100)">
     <rect onclick="remove(evt)" fill="red"
           x="0" y="0" width="200" height="100"/>
  </g>
 </svg>

 














Related examples in the same category
1.Determining Attribute Values of SVG Elements
2.Mouse Clicks and Resizing Circles
3.Mouse Clicks and Scaled Ellipses
4.Dynamically Removing an SVG Element
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.