Sometimes, we want to set the src
sttribute of an element with jQuery.
In this article, we’ll look at how to set the src
sttribute of an element with jQuery.
Set the Src Attribute of an Element with jQuery
To set the src
sttribute of an element with jQuery, we can call the attr
method with the attribute name and value.
For instance, we write:
<img>
to add the img element.
Then we write:
$("img").attr("src", "https://picsum.photos/200");
to select the img element with $
.
Then we call attr
on the returned element with attribute name and value strings.
Now we should see an image displayed.
Conclusion
To set the src
sttribute of an element with jQuery, we can call the attr
method with the attribute name and value.