3

Sorry if this has already been answered, I am new to SO.

I am trying to create svg elements using jquery, and I have this code as part of an HTML page:

<svg viewBox="0 0 1000 500">
    <defs>
        <clipPath id="clip">
            <ellipse cx="100" cy="250" rx="200" ry="50" />
        </clipPath>
    </defs>
    <g>
        <path d="M 0,0 L 1000,0 1000,500 0,500"
            fill="#9ADEFF" />
        <path id="boat" stroke="none" fill="red"
            d="M 100,175 L 300,175 300,325 100,325"
            clip-path="url(#clip)" />
    </g>
    <g id="0002" width="100" height="100%"
        transform="translate(1000)">
        <line x1="50" y1="0" x2="50" y2="300"
            stroke="green" stroke-width="100" />
    </g>
</svg>

and this Javascript (with jQuery 1.9):

var id = 10000,
    coinArray = []

function generateNextLine(type) {
    $('svg').append($(type()))
    return $('svg')[0]
}

function idNo() {
    id++
    return ((id-1)+"").substr(-4)
}

function random(x,y) {
    if (!y) {
        y=x
        x=0
    }
    x=parseInt(x)
    y=parseInt(y)
    return (Math.floor(Math.random()*(y-x+1))+x)
}

function coins() {
    coinArray[id%10000]=[]
    var gID = idNo(), x,
    g=$(document.createElement('g')).attr({
        id: gID,
        width: "100",
        height: "100%"
    })
    while (3<=random(10)) {
        var randomPos=random(50,450)
        coinArray[(id-1)%10000][x] = randomPos
        $(g).append(
            $(document.createElement('circle'))
            .attr({
                cx: "50",
                cy: randomPos,
                r: "50",
                fill: "yellow"
            })
        )
        x++
    }
    return $(g)[0]
}

When I run generateNextLine(coins);, the svg adds this element:

<g id="0000" width="100" height="100%">
    <circle cx="50" cy="90" r="50" fill="yellow"></circle>
</g>

However, the actual display of the svg doesn't change. If I add this code directly to the svg, it renders as I would expect, but running my javascript function does not seem to do anything to the display. I am using Chrome 28, on OS X Lion.

Charlie Harding
  • 653
  • 8
  • 22

1 Answers1

7

You must create SVG elements in the SVG namespace which means you can't do

document.createElement('g')

but in instead you must write

document.createElementNS('http://www.w3.org/2000/svg', 'g')

same for circle etc.

Robert Longson
  • 118,664
  • 26
  • 252
  • 242
  • 2
    Actually, HTML5 allows you to use `` elements directly embedded in HTML without needing a namespace. See [this question](http://stackoverflow.com/questions/3642035/jquerys-append-not-working-with-svg-element) as to why the elements will not show up. **TL;DR**: jQuery's functions are tailored specifically to HTML elements, and many of these functions will fail for SVG elements. For example, jQ uses javascript's `innerHTML`, which is not available to SVG elements. – chharvey Dec 15 '14 at 21:31
  • All true but not relevant to this specific case where the code in the question is using createElement which is why it didn't work. – Robert Longson Dec 15 '14 at 22:23