By default, we use the
<img> tag in HTML to display images. In the
<img> tag, we have a method known as
src, which helps get the source of the image that we gave to display. In the
src, we have to give the complete path of the image; otherwise, it will raise an error. The width, height of the image also needs to be specified.
img tag, which helps us insert an image in our webpage. We can create a reusable function. Here we will be using the
img element again but differently.
We will be creating a function called
display_image(), which will consist of the parameters like
alt of an image in it. We will be using a
createElement() for creating an element in the function. This way, we can dynamically display images wherever we call this function.
The above example displays an image on the webpage based on the provided source. The
document.body.appendChild() function adds the given element to the body of the webpage.