user5033350
user5033350

Reputation:

Change image's src attribute based on JSON

Here is the div:

<div id="img-container">
  <img id="upSlika" src="#"/>
</div>  

And this is the JSON response:

{"pictureUrl":"url"}

How can I change image's source to URL received in response above?

Upvotes: 1

Views: 1494

Answers (2)

bcap
bcap

Reputation: 500

Suppose that your json is stored in the following variable json:

var json = {"pictureUrl": "url"};

With the following code, what you do is to set the attribute src to the element img with id="upSlika":

$("#upSlika").attr("src", json.pictureUrl);

You can check the documentation jQuery ID Selector and jQuery .attr()

Upvotes: 3

nnunes10
nnunes10

Reputation: 550

You can change the image's src in jQuery by using attr() function. See the documentation.

Try the code bellow:

//get the json to data variable
var data = {"pictureUrl":"url"};

$("#upSlika").attr("src", data.pictureUrl);

Upvotes: 1

Related Questions