5

I have created a group using Snap.svg.

shape = paper.rect(225, 50, 100, 50, 10, 10);
text = paper.text(253,82, "text")
myGroup = paper.g(shape, text);
myGroup.click(toggleSelection);

When I actually click the group with my mouse the toggleSelection function is executed. I am unable to figure out how to simulate a click. I've tried myGroup.click() myGroup.events.click() etc.

There doesn't seem to be a built in method to Snap so standard JS is welcome!

Phil
  • 97
  • 1
  • 2
  • 10

1 Answers1

9
var event = document.createEvent("SVGEvents");
event.initEvent("click",true,true);
myGroup.dispatchEvent(event);

This will fire an event that you assigned with .on("click") earlier. This is nicer than manually creating a mouse event with Xs and Ys, as that's difficult and is probably fraught with peril given that your objects overlap each other at times.

MaxZoom
  • 7,619
  • 5
  • 28
  • 44
  • 1
    Thank you! Strangely, it didn't work in my code unless I changed it to myGroup.node.dispatchEvent(event). Which I only figured out because I decided to search for SO questions about Raphael.js (Snap's predecessor) and found this http://stackoverflow.com/questions/9855003/rapha%C3%ABl-object-simulate-click – Phil Jan 21 '15 at 22:21