Syntax error What is the best JavaScript code to create an img element?

What is the best JavaScript code to create an img element?



Try any of the following for the width of the image you want to create −

myImg.setAttribute('width', '5px');

Or

myImg.width = '5';

Or

myImg.style.width = '5px';

You can also try the following code to add the width and height to the background image −

var myImg = new Image(5,5);
myImg.src = 'http://www.example.com';
Updated on: 2020-06-23T12:07:26+05:30

197 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements