Categories
JavaScript Answers jQuery

How to Set the Src Attribute of an Element with jQuery?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *