
Here I will try to teach more to create a dynamic image using JavaScript. Just see this for your purposes. Now I create an image using structure using HTML and CSS then showing up create a dynamic image using JavaScript framework.
<!DOCTYPE HTML>
<html>
<head>
<title>
Create a dynamic image using JavaScript ?
</title>
</head>
<body id = “body” style = “text-align:center;”>
<h1 style = “color:#ff0089;” >
WigMarketing
</h1>
<p id = “GFG_UP” style =
“font-size: 15px; font-weight: bold;”>
</p>
<button onclick = “GFG_Fun()”>
Touch this
</button>
<p id = “GFG_DOWN” style =
“color:#ff0089; 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 = ‘https://wigmarketing.com/wp-content/uploads/2021/12/WigMarketing-Digital-Services-2.png’;
document.getElementById(‘body’).appendChild(img);
down.innerHTML = “Image Element Added.”;
}
</script>
</body>
</html>
Done this process, then open this code on a browser, you will be able to see this output on this coding. It helps to create a dynamic image.
After clicking this image button, you will see this on your browser.
Important Elements of create dynamic image using JavaScript
Slow image in plain HTML
First of all, you need to create a static image tag using the src attribute image link with HTML.
You must need to know the basics of image and this usage process. I recommended every developer know they’re using the process on images.
Set up one or more src attribute in JavaScript
In these HTML files, you must create an image src attribute in JavaScript image tag like <img> for the link-up or set up one or more src attribute images on your website then assign a URL to your src images. You can do another way to set up an image tag using square bracket syntax on your selected image. Using like this
img[“src”]=” image url.png”;
Build up image Element in Javascript
Using methods of document objects to create an image element like createElement() and setup src attribute on this image. Using code to create this-
cont img=docmunt.createElements(“img”);
img.src= “image url.png”;
document.body. appendChild(img);
Now you are able to add the element to the DOM hierarchy on body elements.
Add inline image style in JavaScript
Using the JavaScript property, add a style to the image element directly. Here you want to add border radius on your selected image on your site. Just follow this codes on your website let.img=document.createElement(”img”) ;
img.src=”image url ”;
img.style.border=”12px double pink ” ;
img.border radius=”8px”;
Using ID attribute in JavaScript language
JS provides a lot of feature options here you can add multiple styles to the image elements. Here you create a new CSS rule style tag or external CSS file to the ID selector on our website. Then assign an ID to the image element of the ID property.
Img-rounded-border{
border=”12px solid blue”;
border-radius=”8px”;
img-id=”img-rounded-border”;
}
If you want, you can complete this process using another way just attribute() on the image object like-
img.setAttribute(”id”, img-rounded-border);
In this article, you will help to create any kind of image to provide a dynamic look that makes your website more attractive.