1

I already created an SVG, Now my problem is to add a few codes dynamically in between two elements.

How can I achieve it? even JS or jquery no problem!

DEMO

<svg width="500" height="500">  
  <circle id="circle1" class="sid" cx="50" cy="50" r="20" fill="red"/>
  <circle id="circle2" class="sid" cx="150" cy="50" r="20"  fill="green"/>
  <circle id="circle4" cx="350" class="sid" cy="50" r="20" fill="yellow"/>  
</svg>

$(document).ready(function() {
    $(".sid").mouseenter(function() {
      alert("I'm here");
$('#circle2').append('<circle id="circle3" cx="250" class="sid" cy="50" r="20" fill="blue"/>');  

     });
    $(".sid").mouseleave(function() {
       // action
     });
});

is it possible instead of creating SVG adding few codes inside of elements between?

Expecting output like.

<svg width="500" height="500">  
  <circle id="circle1" class="sid" cx="50" cy="50" r="20" fill="red"/>
  <circle id="circle2" class="sid" cx="150" cy="50" r="20"  fill="green"/>
  <circle id="circle3" cx="250" class="sid" cy="50" r="20" fill="blue"/>
  <circle id="circle4" cx="350" class="sid" cy="50" r="20" fill="yellow"/>  
</svg>
Siddhartha esunuri
  • 1,104
  • 1
  • 17
  • 29

2 Answers2

3

To create a SVG element, you have to declare the namespace which is http://www.w3.org/2000/svg.

Here's a demo:

$(document).ready(function() {
  $('svg').append(createSVG('circle', {
    id: "circle3",
    cx: 250,
    cy: 50,
    r: 20,
    fill: "blue",
    class: "sid"
  }));
});

function createSVG(tag, attrs) {
  var svg = document.createElementNS('http://www.w3.org/2000/svg', tag);
  for (var attr in attrs)
    svg.setAttribute(attr, attrs[attr]);
  return svg;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<svg width="500" height="500">  
  <circle id="circle1" class="sid" cx="50" cy="50" r="20" fill="red"/>
  <circle id="circle2" class="sid" cx="150" cy="50" r="20"  fill="green"/>
  <circle id="circle4" cx="350" class="sid" cy="50" r="20" fill="yellow"/>  
</svg>

Or parses it into an XML document and append the documentElement from the XML.

$(document).ready(function() {
  $("svg").append(
    $.parseXML(`<circle xmlns="http://www.w3.org/2000/svg" 
    id="circle3" cx="250" class="sid" 
    cy="50" r="20" fill="blue"/>`).documentElement);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<svg width="500" height="500">  
  <circle id="circle1" class="sid" cx="50" cy="50" r="20" fill="red"/>
  <circle id="circle2" class="sid" cx="150" cy="50" r="20"  fill="green"/>
  <circle id="circle4" cx="350" class="sid" cy="50" r="20" fill="yellow"/>  
</svg>
Hikarunomemory
  • 4,237
  • 2
  • 11
  • 21
2

try this code, i have already checked in your fiddle

   $(document).ready(function() {
   // $(".sid").mouseenter(function() {
      //alert("I'm here");
      var a = document.createElementNS('http://www.w3.org/2000/svg','circle')
      console.log(a)
      a.setAttribute("class", 'sid')
      a.setAttribute("r", 20)
      a.setAttribute("cy", 50)
      a.setAttribute("cx", 250)
      a.setAttribute("fill", "blue")
      a.setAttribute("id", "circle3")
   $('#circle2').after(a); 
   //  });
    $(".sid").mouseleave(function() {
       // action
     });
});
Arif Rathod
  • 578
  • 2
  • 13