Gemelo Molinero
Gemelo Molinero

Reputation: 117

How to replace an image in an embedded svg?

I am trying to dynamically replace an image in an embedded svg, I can get the current href attribute but setting it to a new one doesn´t change anything. Does this even work at all, as it is not a usual html img tag?

Here a (reduced to the essentials) code excerpt:

<script>
  alert(document.getElementById("image1").getAttribute('href'));
  // this works but document.getElementById("image1").setAttribute('href')='newimage.jpg'; doesn´t do anything
</script>

<svg>
    <image id="image1" href="test.jpg"></image>
</svg>

Upvotes: 0

Views: 240

Answers (1)

Marik Ishtar
Marik Ishtar

Reputation: 3049

Try this

document.getElementById("image1").setAttribute('href', 'newimage.jpg')

Upvotes: 2

Related Questions