Manually firing events for an SVG Element?

9,385 views
Skip to first unread message

Aleksey

unread,
Aug 13, 2012, 6:27:03 AM8/13/12
to d3...@googlegroups.com
All,

I've done some digging, but I haven't found a simple (enough for me) solution to my problem. Not an advanced JavaScript programmer here.

I have some SVGGElements that I have an on("click") event for when I entered them.

I want to manually fire this event programmatically under some other cricumstances.

I can get as far as getting my hands on the SVGGElement and even the .on("click") for it, but I'm at a loss as to what to do from there.

Maybe I just don't know enough to fill in these blanks: .on("click")( ____ , ____) where my function is expecting a d and an i?

Pardon the noobery ;)

Thanks,
Aleksey

jerome cukier

unread,
Aug 13, 2012, 6:40:18 AM8/13/12
to d3...@googlegroups.com
well, you can do the following

instead of writing 
myelement.on("click", function(d,i) { /* does stuff */ });

you can write: 
myelement.on("click", action);

and then

function action(d,i) { /* does stuff */ }

and subsequently call the function action from anywhere. 
by the way, if you don't need to make reference to the data underlying your element or to its rank among similar element feel free to drop the (d,i) altogether. (i.e. function action() {...})

Aleksey

unread,
Aug 13, 2012, 6:53:36 AM8/13/12
to d3...@googlegroups.com
I will drop the "i", thank you, but I will need the d.

I think carving out the action() as below accomplishes then same thing as getting my hands on the .on("click").

With just the (d) now, how do I call the function?

action( ____ )?

Jinling Li

unread,
Aug 13, 2012, 10:17:30 AM8/13/12
to d3...@googlegroups.com
REMOVE ME

Kai Chang

unread,
Aug 13, 2012, 10:41:19 AM8/13/12
to d3...@googlegroups.com
You can unsubscribe from this list within gmail. Click the little arrow next to "d3-js" then "Unsubscribe to this mailing-list"

Aleksey, it sounds like part of your question involves simulating a click event. d3 doesn't have this built-in, but jQuery does:


Without jQuery, use dispatchEvent.




Simplest example of dispatchEvent I found:

Ian Johnson

unread,
Aug 13, 2012, 10:58:10 AM8/13/12
to d3...@googlegroups.com
If you have access to the g element you want with a d3 selection you can do

var selection = d3.select(...);

selection.each(function(d,i) {
 action(d);
})
--
Ian Johnson

Aleksey

unread,
Aug 13, 2012, 11:00:12 AM8/13/12
to d3...@googlegroups.com, kai.s...@gmail.com
Great! Thanks Jerome and Kai.

My solution:

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

This then fires the event that I 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 my objects overlap each other at times.

Amol Khatri

unread,
Jan 31, 2014, 1:12:47 AM1/31/14
to d3...@googlegroups.com, kai.s...@gmail.com
Thnaks Aleksey. It worked perfectly for me.

Manny

unread,
Apr 24, 2014, 2:33:37 PM4/24/14
to d3...@googlegroups.com, kai.s...@gmail.com
I am facing the same issue on SVG "g" element.

var event = document.createEvent("SVGEvents");
event.initEvent("click",true,true);
var d = document.getElementById("a0xa000033"); // id for one the "g" element.
d.dispatchEvent(event);

Event didn't fire (checked in FF)

But I am able to fire the same event for an "image" tag under the "g" element.

Any insight appreciated.

Thanks.
Reply all
Reply to author
Forward
0 new messages