I am trying to use JQuery to append some svg elements. Code as shown below.
$("#save_edits").click(function() {
var img = document.createElementNS('http://www.w3.org/2000/svg','image');
img.setAttributeNS('http://www.w3.org/1999/xlink','href','./img/exclamation-mark.png');
img.setAttributeNS(null,'x','-7');
img.setAttributeNS(null,'y','-35');
img.setAttributeNS(null,'height','15');
img.setAttributeNS(null,'width','15');
if(id==" Shipping"){
$('svg>g').first().append(img);
$('svg>g:eq(2)').append(img);
$('svg>g:eq(3)').append(img);
$('svg>g:eq(4)').append(img);
}
});
However, only the last append works successfully $('svg>g:eq(4)').append(img);
, and not all the previous. Does anyone know why previous ones were override and how to prevent it from occurring?
Answer: Ok, seems like I figured out eventually. Basically need to recreate the img element again for each append.
child = ["svg>g:eq(2)","svg>g:eq(3)","svg>g:eq(4)"]
if(id==" Shipping"){
child.forEach(function(e){
var img = document.createElementNS('http://www.w3.org/2000/svg','image');
img.setAttributeNS('http://www.w3.org/1999/xlink','href','./img/exclamation-mark.png');
img.setAttributeNS(null,'x','-7');
img.setAttributeNS(null,'y','-35');
img.setAttributeNS(null,'height','15');
img.setAttributeNS(null,'width','15');
$(e).append(img);
})