3

I am working with snap svg and I see in the docs that there is a way to append() and remove() the elements, but I don't have clear how to adapt it to my code

_placeBet = () => {
  const tipChipSnap = snap('#chip-bet');
  const tipChipSvgContent = Snap.parse(this.props.chipSelectedSvg.content);
  tipChipSnap.append(tipChipSvgContent);
}

The append() method is working as expected, all I need is to know how to use remove().

The reason why I need to remove the append it element, is because at some point I will have more than 100 elements of the same in the DOM and I want to avoid that. So lets say you do _placeBet() and then tipChipSnap.append(tipChipSvgContent); is fired appending a new element, I need that everytime a new element is added, delete the last one and just keep with the new one.

So, what are your recommendations ?

Buzinas
  • 11,597
  • 2
  • 36
  • 58
Non
  • 8,409
  • 20
  • 71
  • 123

1 Answers1

1

You can save the current saved element, so the next time the function is called, you remove it. E.g:

let previous;
_placeBet = () => {
  const tipChipSnap = snap('#chip-bet');
  const tipChipSvgContent = Snap.parse(this.props.chipSelectedSvg.content);
  tipChipSnap.append(tipChipSvgContent);

  if (previous) previous.remove();
  previous = tipChipSvgContent;
}
Buzinas
  • 11,597
  • 2
  • 36
  • 58
  • with this method I am able to add only the first element, but when I try to add a second one, this isn't working. – Non Sep 26 '15 at 02:19
  • could you paste your full code into [PasteBin](http://pastebin.com/), so I can take a look? – Buzinas Sep 26 '15 at 02:27
  • @NietzscheProgrammer sorry, man. It was a typo. The variable `tipChipSvgContent` should be saved into the `previous` variable, and not into `tipChipSnap`, as my answer said. I've edited my answer, it should work now. – Buzinas Sep 26 '15 at 02:41
  • should I put `previous = tipChipSvgContent;` into the `if` ? – Non Sep 26 '15 at 02:52
  • @NietzscheProgrammer no, the `if` is only for the first time, since there will be no previous. – Buzinas Sep 26 '15 at 03:22