I am having some trouble with using the function Element.append(el) in the Snap.svg API. I need to store an instance of that SVG in an array of variables so that I can access specific ones throughout runtime.
When I try this code with just storing it into groupViews, it works. But, putting it an array (groupView) brings me this error:
Uncaught TypeError: Cannot read proerty 'append' of undefined
The console shows me the same output when I log groupViews and groupView[i] so I am not sure what else to try.
Any ideas as to how I can solve this? Thanks.
for(var i = 0; i < numOfGroups; i++)
{
var sel = '#GroupView' + i;
groupView[i] = Snap(sel);
console.log(groupView[i]);
groupViews = Snap('#GroupView0');
console.log(groupViews);
Snap.load("/svg/groupView-12_13_2016-01.svg", function(f) {
groupView[i].append(f);
groupViews.append(f);
}); //end of Snap.load(groupView)
}
Edit (1/5):
Here is the HTML code where I have empty SVGs loaded:
<div class = "groupView0">
<svg viewBox='0 0 3640.9 540.5' id='GroupView0'></svg>
</div>
<div class = "groupView1">
<svg viewBox='0 0 3640.9 540.5' id='GroupView1'></svg>
</div>
Hence, this is why I am loading the specific SVG file and appending it in JS. Is there a better way to do this?
Edit (1/6):
Here is a small portion of my svg file:
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 20.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 3640.9 540.5" style="enable-background:new 0 0 3640.9 540.5;" xml:space="preserve">
<g id="A" onclick="detailedView();" cursor="pointer">
<rect id="PanelBackground" x="35.4" y="55.5" class="st2" width="173.2" height="155"/> <!--height="444" -->
<g id="UpperBar">
<linearGradient id="Header_20_" gradientUnits="userSpaceOnUse" x1="35.3937" y1="67.0279" x2="208.5774" y2="67.0279">
<stop offset="0" style="stop-color:#F68E1E"/>
<stop offset="0.5" style="stop-color:#F16A22"/>
<stop offset="0.903" style="stop-color:#F05F22"/>
<stop offset="1" style="stop-color:#F05C22"/>
</linearGradient>
<polygon id="Header" class="st3" points="35.4,55.5 208.6,55.5 208.6,78.6 35.4,78.6 "/>
<g>
<rect x="82.1" y="61.8" class="st4" width="121" height="10.8"/>
<text id="GroupName" transform="matrix(1 0 0 1 82.0885 71.6627)" class="st5 st6 st7">A</text>
<text id="FloorNumber" transform="matrix(1 0 0 1 182.3287 141.8773)" class="st2 st19 st7">X</text>
</g>
</g>
</svg>
After executing the code you posted.. If I try to run code like:
groupName[0] = groupView[0].select('#GroupName');
groupName[0].attr({
text: "something"
});
I get this error: Uncaught TypeError: Cannot set property '0' of undefined
Try something like this, it uses clone() as it looks like you are loading in the same SVG file each time, so we could load it in once, then clone() it to save repeated calls to the server.
This relies on the basis that the svg file will have an outer markup in it. If it's partial svg, and only has a element, change the select('svg') to select('group') or whatever it is.
Then we store a reference to the clone, and then append that element to the indexed svg element.
Here is a full example, the svg test.svg loaded in is simply..
Main html/js
Working example