Creating an SVG in Javascript, trivial example

I’m no JS coder, but for my reference below is a simple example showing how to create an SVG image in Javascript. The code below also shows how the SVG element itself might be created in JS.

UPDATE: If you’re looking at creating SVGs in Javascript, I recommend checking out FabricJS, I found it much easier to get what I wanted working.


<script language="javascript">

var svgNS = "";  

function do_draw() {

  // The following commands can be used to create an SVG programatically
  // var mySVG = document.createElementNS("", "svg");
  // mySVG.setAttribute("height",300);
  // mySVG.setAttribute("width" ,300);
  // document.body.appendChild(mySVG);
  var mySVG = document.getElementById('mySVG')

  var circles = document.createElementNS("", "circle");


<svg id="mySVG" style="width:300px; height:300px" xmlns="" xmlns:xlink=""/>
<input type="button" id="drawBtn" value="draw" onclick="do_draw()"></input><br><br>
<div id='main'></div>