Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6901111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:37:56+00:00 2026-05-27T07:37:56+00:00

How do I get the SVG DOM root, and start adding children to it,

  • 0

How do I get the SVG DOM root, and start adding children to it, programmatically, instead of adding SVG elements by markup?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:37:57+00:00Added an answer on May 27, 2026 at 7:37 am
    <svg xmlns="http://www.w3.org/2000/svg">
      <script>
        var svg   = document.documentElement;
        var svgNS = svg.namespaceURI;
    
        var circle = document.createElementNS(svgNS,'circle');
        circle.setAttribute('cx',100);
        circle.setAttribute('cy',200);
        circle.setAttribute('r',50);
        circle.setAttribute('fill','red');
        circle.setAttribute('stroke','black');
        circle.setAttribute('stroke-width','20px');
        circle.setAttribute('stroke-opacity','0.5');
    
        svg.appendChild(circle);
      </script>
    </svg>
    

    Seen in action: http://phrogz.net/SVG/create-circle.svg

    Because it can be so cumbersome to issue all those setAttribute calls, I often use a function like this:

    // Create an SVG element on another node with a set of attributes.
    // Attributes with colons in the name (e.g. 'xlink:href') will automatically
    // find the appropriate namespace URI from the SVG element.
    // Optionally specify text to create as a child node, for example
    //   createOn(someGroup,'text',{x:100,'text-anchor':'middle'},"Hello World!");
    function createOn(root,name,attrs,text){
      var doc = root.ownerDocument, svg = root;
      while (svg.tagName!='svg') svg = svg.parentNode;
      var el = doc.createElementNS(svg.namespaceURI,name);
      for (var a in attrs){
        if (!attrs.hasOwnProperty(a)) continue;
        var p = a.split(':');
        if (p[1]) el.setAttributeNS(svg.getAttribute('xmlns:'+p[0]),p[1],attrs[a]);
        else el.setAttribute(a,attrs[a]);
      }
      if (text) el.appendChild(doc.createTextNode(text));
      return root.appendChild(el);
    }
    

    In action, the above file becomes more simply:

    <svg version="1.1" baseProfile="full" xmlns="http://www.w3.org/2000/svg">
      <script>
        var svg = document.documentElement;
        createOn(svg,'circle',{
          cx:100, cy:200, r:50,
          fill:'red', stroke:'black',
          'stroke-width':'20px', 'stroke-opacity':0.5
        });
        // …function from above…
      </script>
    </svg>
    

    Seen in action: http://phrogz.net/svg/create-circle-2.svg

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get the position of SVG elements using Javascript. I have
What's the best way to add SVG elements to the dom in Clojurescript ?
I've been trying for the past 3 hours to get my svg web site
I want to run Protovis javascript from Java and get the evaluated SVG code.
I'm toying with Java and SVG Salamander but can't quite get how to render
I'm working on drawing an SVG grid using Javascript. I've managed to get the
I'm working on some ECMAScript/JavaScript for an SVG file and need to get the
For some reason I can't get my SVG filters to work in Firefox. They
I am trying to scale a picture drwable which I get from svg-android-library .
I've been really trying to learn some SVG. But browsers seem to get in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.