1
I have this code to dynamically create SVG elements through a for, and a step number, which is the number of elements that will be created.(default equals 10). I add an id to each element. I would now like to know if for example I click on one of the elements to know their id. To then get the position of this element.
for (var i = 0; i < steps; i++) {
var x = stepX * (i + 1);
var y = stepY * (i + 1);
//create circle
var shape = document.createElementNS(
"http://www.w3.org/2000/svg", "circle");
shape.setAttributeNS(null, "cx", originX + x);
shape.setAttributeNS(null, "cy", originY - y);
shape.setAttributeNS(null, "r", 5);
shape.setAttributeNS(null, "fill", "green");
shape.setAttributeNS(null, "class", "draggable");
shape.setAttributeNS(null, "order", i);
shape.setAttributeNS(null, "id", i);
shape.id="circle"+i;
svg.appendChild(shape);
document.write(shape.id);
shape.onmousedown = function(evt) {
var evt = evt || window.event;
ddData.element = evt.target || evt.srcElement;
ddData.initialX = evt.clientX;
ddData.initialY = evt.clientY;
ddData.originalX = parseFloat(
ddData.element.getAttributeNS(null, "cx"));
ddData.originalY = parseFloat(
ddData.element.getAttributeNS(null, "cy"));
var order = parseInt(
ddData.element.getAttributeNS(null, "order"));
ddData.lineEnd = lines[order];
ddData.lineStart = lines[order+1];
};
}
I use the Document.getElementById('circle0') which supposedly is the first circle, but does nothing.
Right, but if I click on the other circles also appears the Alert, I just wanted it to appear in a circle, in the one that is identified by the id.
– akm
After you have entered the svg with the circles on the page, at any time you can assign the click event.
– Édipo Costa Rebouças
http://jsfiddle.net/kyLuLshz/
– Édipo Costa Rebouças
Exactly, and if I want to change the radius of only one of the circles, just use your id?
– akm
I believe so, I don’t know very well how to work with svg, but you can catch it by id yes.
– Édipo Costa Rebouças
see this example: http://jsfiddle.net/7639nywf/
– Édipo Costa Rebouças