Referencing using CSS selectors


returns SVG.Dom (or the most relevant subclass of SVG.Element)

This function will get the first element matching the passed selector

var rect = SVG('').fill('#f06')


returns SVG.Dom (or the most relevant subclass of SVG.Element)

This function will get the first element in element matching the passed selector

var rect = group.findOne('').fill('#f06')


returns SVG.List

This will search for all svg elements matching the selector and return them in an instance of SVG.List:

var list = SVG.find('.someClass')


It also allows for a second parameter that is a node in which should be searched:

var list = SVG.find('.someClass', group)



returns SVG.List

This will search for all svg elements matching the selector below element and return them in an instance of SVG.List:

var list = group.find('.someClass')


Referencing existing DOM elements


returns SVG.Element

If you want SVG.js to adopt an existing DOM element, you can use the SVG():

var polygon = document.createElementNS('', 'polygon')

var element = SVG(polygon)


Using jQuery or Zepto

Another way is to use jQuery or Zepto. Here is an example:

// add elements
var draw   = SVG().addTo('#drawing')
var group  ='my-group')
var rect   = group.rect(100,100).addClass('my-element')
var circle ='my-element').move(100, 100)

// get elements in group
var elements = $('#drawing .my-element').each(function() {

Circular reference

Every element instance within SVG.js has a reference to the actual node.


returns SVGElement



returns SVG.Element

Similarly, the node carries a reference to the SVG.js instance:


Note the difference in return values.
The node references a native SVGElement while instance references the SVG.Element instance.

Child references


returns array

An SVG.List of all children can be retrieved with the children method:



returns itself

To remove all elements from a parent element:



returns itself

The each() allows you to iterate over the all children of an element:

draw.each(function(i, children) {
  this.fill({ color: '#f06' })

Deep traversing is also possible by passing true as the second argument:

// draw.each(block, deep)
draw.each(function(i, children) {
  this.fill({ color: '#f06' })
}, true)

Note: this refers to the current child element.


returns SVG.Element

To get the first child of a parent element:



returns SVG.Element

Get an element on a given position in the children array:

var rect   = draw.rect(20, 30)
var circle =

draw.get(0) //-> returns rect
draw.get(1) //-> returns circle


returns boolean

Checking the existence of an element within a parent:

var rect  = draw.rect(100, 50)
var group =

draw.has(rect)  //-> returns true
group.has(rect) //-> returns false


returns number

Returns the index of given element and returns -1 when it is not a child:

var rect  = draw.rect(100, 50)
var group =

draw.index(rect)  //-> returns 0
group.index(rect) //-> returns -1


returns SVG.Element

To get the last child of a parent element:


Parent references

Every element has a reference to its parent with the parent() method:


returns SVG.Svg

For retrieving the root SVG you can use root()

var draw = SVG().addTo('#drawing')
var rect = draw.rect(100, 100)

rect.root() //-> returns draw

parent() within the svg document

returns SVG.Element


Alternatively a class or css selector can be passed as the first argument:

var draw   = SVG().addTo('#drawing')
var nested = draw.nested().addClass('test')
var group  =
var rect   = group.rect(100, 100)

rect.parent()           //-> returns group
rect.parent(SVG.Svg)    //-> returns nested
nested.parent(SVG.Svg)  //-> returns draw
rect.parent(SVG.G)      //-> returns group
rect.parent('.test')    //-> returns nested

parent() on the topmost svg document

returns HTMLNode

var draw = SVG().addTo('#drawing')

draw.parent() //-> returns an SVG.Dom wrappig the html element with id 'drawing'


returns SVG.List

To get all ancestors of the element up to and including the passed matcher or element use parents().
If no matcher is passed, the root svg element is used.

var group1 ='test')
  , group2 =
  , rect   = group2.rect(100,100)

rect.parents()        // returns [group2, group1, draw]
rect.parents('.test') // returns [group2, group1]
rect.parents(group2)   // returns [group2]
rect.parents(group1)   // returns [group2, group1]

URI references

In cases where an element is linked to another element through an attribute, the linked element instance can be fetched with the reference() method.


returns SVG.Element

The only thing required is the attribute name:

use.reference('href') //-> returns used element instance
// or
rect.reference('fill') //-> returns gradient or pattern instance for example
// or
circle.reference('clip-path') //-> returns clip instance

Creating Elements


To create a new SVG.Svg document, just call SVG()

var draw = SVG()

To create an element from markup you can also use SVG()

var rect = SVG('<rect width="100" height="100">')

object constructor

To create a bare svg object which is not attached to the dom use the elements constructor directly

var rect = new SVG.Rect()
Fork me on GitHub