Given an HTML element and the task is to create an <img> element and append it to the document using JavaScript. In these examples when someone clicks on the button then the <img> element created. We can replace click event by any other JavaScript event.
Approach 1:
- Create an empty img element using document.createElement() method.
- Then set its attributes like (src, height, width, alt, title etc).
- Finally, insert it into the document.
Example 1: This example implements the above approach.
<!DOCTYPE HTML> <html> <head> <title> How to create an image element dynamically using JavaScript ? </title> </head> <body id = "body" style = "text-align:center;"> <h1 style = "color:green;" > GeeksforGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <button onclick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); up.innerHTML = "Click on the button to add image element"; var down = document.getElementById('GFG_DOWN'); function GFG_Fun() { var img = document.createElement('img'); img.src = document.getElementById('body').appendChild(img); down.innerHTML = "Image Element Added."; } </script> </body> </html> |
Output:
- Before clicking on the button:

- After clicking on the button:

Approach 2:
- Create an empty image instance using new Image().
- Then set its attributes like (src, height, width, alt, title etc).
- Finally, insert it to the document.
Example 2: This example implements the above approach.
<!DOCTYPE HTML> <html> <head> <title> How to create an image element dynamically using JavaScript ? </title> </head> <body id = "body" style = "text-align:center;"> <h1 style = "color:green;" > GeeksforGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <button onclick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); up.innerHTML = "Click on the button to add image element"; var down = document.getElementById('GFG_DOWN'); function GFG_Fun() { var img = new Image(); img.src = document.getElementById('body').appendChild(img); down.innerHTML = "Image Element Added."; } </script> </body> </html> |
Output:
- Before clicking on the button:

- After clicking on the button:

Recommended Posts:
- How to Change Image Dynamically when User Scrolls using JavaScript ?
- How to Create a Form Dynamically with the JavaScript?
- How to dynamically create new elements in JavaScript ?
- How to change style attribute of an element dynamically using JavaScript ?
- Create a Form Dynamically using Dform and jQuery
- Change Image Dynamically when User Scrolls Down | Set 2
- How to create button dynamically with click event in Angular ?
- How to create dictionary and add key–value pairs dynamically?
- How to dynamically create '@-Keyframe' CSS animations?
- How to dynamically set the height and width of a div element using jQuery ?
- How to set id attribute of an element dynamically using AngularJS ?
- How to dynamically change the title of web page using JavaScript ?
- How to merge properties of two JavaScript objects dynamically?
- How to get the javascript function parameter names/values dynamically?
- How to add elements to an existing array dynamically in JavaScript ?
- Dynamically Execute JavaScript in ElectronJS
- Dynamically generating a QR code using PHP
- How to dynamically get the content width of a div using AngularJS ?
- How to generate an XML file dynamically using PHP?
- How to Dynamically Add/Remove Table Rows using jQuery ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


