Assign JSON data to the src attribute of an <img> element

$("#id").html(JSON.obj);

if the above code stores a JSON object into the element with id=“id” as text to the html page.

how do I store the JSON object in this case a URL into an image element’s src attribute ??

$("#id").src(JSON.obj) ?? logic but i know its not right.

Since src is an attribute of $("#id") you can use .attr() to change it.

1 Like