I got this SVG image from Wikipedia and embedded it into a website using this code:
<embed src="circle1.svg" type="image/svg+xml"/>
If you run this, you can inspect the element and see the source code. All countries in the image are separate elements. If I click on a country, I want to alert the id of that country, since every country has an id of two letters in the SVG. Does anyone know a way to do this? Would it be easier if I place it into a element?
Okay using your comments I found an answer to my problem. I added this code in the svg itself.
The function addClickEvents is triggered when the svg loads. But I still have another problem with this. I have to embed this svg (with the code) into a HTML document using
Instead of alerting the id, I have to place it into a div in the HTML document. How do I get this id from the svg?
If your SVG is contained in the DOM, you can attach event listeners to separate elements in the same manner as basic HTML. Using jQuery one example would be: http://jsfiddle.net/EzfwV/
Update: Most modern browsers support inline svg, so to load your source into the DOM, all you have to do is load it in from a resource, (using something like jQuery's load()).
Edit: more specific example http://jsfiddle.net/EzfwV/3/
Update July 2016
It is possible to respond to events from
embed
elements now, under the condition that you embed something from the same domain. I have created a quick demonstration of this as a JSFiddle. The most important part is that it is possible to access the embedded document throughembeddingElement.contentDocument
. From there, the SVG element can be accessed and click event handlers can be installed.Implementation note: in the demo, I add event handlers to all
path
elements. For performance reasons, you would probably want to add a single event handler to the SVG and then use the event target in the handler. Edit: like in this updated Fiddle.Old answer
A quick Google search brought me here. I think that's the answer to your problem, right?
To summarize here: it is not possible to capture events on an
embed
element, unfortunately the only solution is modifying the SVG file.Here is a small example of how to embed JavaScript into an SVG file (JSFiddle). It is based on an example from IBM developerWorks.
Here's an example that should be very similar to what you're trying to do. It uses <object> instead of <embed>, but that's only a minor detail. See this other example for how to get to the DOM of the embedded document from the parent document, it differs just a bit between the two.
Also note that the svg maps that wikipedia have are quite large, so you will want to optimize the svgs before actual usage on a live website, try e.g SVG Cleaner or SVG Scour.
A simple method is
Script
Live Demo in JSFiddle