Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an SVG text element "click-through-able"?

Tags:

javascript

svg

I have a map with SVG text elements to name the locations. I want the locations (shapes) to be clickable, and they are, but because the text elements are on top of them, if someone hovers over a text element and clicks, then nothing happens because the shape was not clicked : the text element was. How can I make it so that if the text element is clicked, the click goes "through" it and to the shape ?

like image 476
Cystack Avatar asked Oct 16 '12 00:10

Cystack


People also ask

How do I make SVG text editable?

But most importantly, what's the best way to make SVG text editable? document. getElementById("element"). contentEditable = "true"; You can also use the ref contenteditable="true" in an HTML element like so: <div contenteditable="true"> .

How do I click an SVG element?

The SVG element has the tag name svg. It has attributes like width, height, viewBox, and so on. To click the element with svg, we should identify the element then utilize the Actions class. We shall first move to that element with the moveToElement method and then apply the click method on it.

Is SVG text editable?

SVG 1.2 introduces editable text fields, moving the burden of text input and editing to the user agent, which has access to system text libraries.

Can SVG contain text?

SVG treats a text element in much the same way that it treats graphical and shape elements. You position text with x and y coordinates and use fill and stroke options to color text the same way you would with a <circle> or <rect> element.


1 Answers

Mozilla introduced a CSS property for this purpose called pointer-events. It was originally limited to SVG shapes, but is now supported on most DOM elements in modern browsers:

span.label { pointer-events: none; } 
  • https://developer.mozilla.org/en-US/docs/CSS/pointer-events
  • http://caniuse.com/pointer-events

The answer to this question has some good information on achieving the same result in old IE:

css 'pointer-events' property alternative for IE

like image 75
Matt Stone Avatar answered Oct 06 '22 22:10

Matt Stone