user994165
user994165

Reputation: 9512

Adding the :hover pseudoclass

Is there any way to programmatically have D3 add a ":hover" to a selection? If not, how can I do this using straight JavaScript?

Upvotes: 0

Views: 194

Answers (2)

Peter
Peter

Reputation: 153

As already explained by cdrini, it is not exactly possible to accomplish this by JavaScript.

Instead, you can use this code to add a class to an element on hover:

element.onmouseover = function(){ this.classList.add('foo'); };
element.onmouseout = function(){ this.classList.remove('foo'); };

Upvotes: 0

cdrini
cdrini

Reputation: 1096

You can't programmatically add :hover from JavaScript (or D3). I'd recommend using a CSS class with the same styling rules:

#foo:hover, #foo.selected { ... }

And then add the .selected class from D3.

(See: How do I simulate a mouseover in pure JavaScript that activates the CSS ":hover"? )

Upvotes: 3

Related Questions