Shanmuga kumar
Shanmuga kumar

Reputation: 175

How to change the image url in svg using jquery?

I want change image url dynamically based id attribute using jquery below i given the svg tag. any one help me.

<image id="background_image" width="804" height="943" preserveAspectRatio="xMinYMin" style=" pointer-events:none;background-repeat:repeat-y;" xlink:href="http://www.example.com/img/imag.png">

i want change dynamically the blow url http://www.example.com/img/imag.png

Upvotes: 2

Views: 1531

Answers (2)

Pugazh
Pugazh

Reputation: 9561

Use the jQuery .attr() to update the href.

$(function() {
  $('#background_image').attr('xlink:href', 'http://placehold.it/350x150');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<image id="background_image" width="804" height="943" preserveAspectRatio="xMinYMin" style=" pointer-events:none;background-repeat:repeat-y;" xlink:href="http://www.example.com/img/imag.png">

Upvotes: 1

Yasser Shaikh
Yasser Shaikh

Reputation: 47804

A simple selector with attr like the one below should work for you.

$('#background_image').attr('xlink:href','newurl.com/image.png');        

Upvotes: 3

Related Questions