1

I have a number of <image> elements in my SVG. I want to add an onclick handler to them that accesses the image URL associated with the specific node, but I can't find the field name in the DOM documentation. If it was HTML then I would be accessing this.src, but the SVG DOM is clearly different -- I can't find any relevant documentation on this.

Added clarification: if I have something like <image xlink:href="http://example.jpg" onclick="code"> then I need the 'code' to access the image URL for the current node.

3 Answers 3

1

OK, both of the following seem to work, although I'm not sure which is recommended:

onclick="window.open(this.getAttribute('xlink:href'),'_blank');"

onclick="window.open(this.getAttributeNS('http://www.w3.org/1999/xlink', 'href'), '_blank');"

Sign up to request clarification or add additional context in comments.

Comments

0

You can use handlers for SVG in <object> tag:

<object type="image/svg+xml" data="picture.svg">
  <img src="picture.png" alt="">
</object>

1 Comment

That's not what I need to do. My SVG includes lots of JPG thumbnails in rect elements, using image elements. I want to add "onclick=..." handlers on each one that accesses the current image-file URL (to view in a different tab). I don't want to hand-code the image-file URL on each onclick attribute.
0

SVG DOM properties have one extra trick because many can be animated: add an extra .baseVal (for the non-animated value) to the property name. In the case of href, you can leave off the xlink: part.

onclick="window.open(this.href.baseVal, '_blank');"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.