- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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';
- Related Articles
- Which is the best tool to debug JavaScript/jQuery code?
- JavaScript code to print last element of an array
- What is the best way to add an event in JavaScript?
- How to create an element from a string in JavaScript?
- What is the best way to initialize a JavaScript number?
- What is the best way to concatenate strings in JavaScript?
- How to create a new img tag with JQuery, with the src and id from a JavaScript object?
- The Best C++ Code Formatter/Beautifier?
- What is the best way to search for an item in a sorted list in JavaScript?
- What is the best way to compare two strings in JavaScript?
- What is the best way to initialize a JavaScript Date to midnight?
- Create a syntax highlighting code with JavaScript.
- What is the best way to do optional function parameters in JavaScript?
- What is the best way to break from nested loops in JavaScript?
- What is the best part of being an Indian?

Advertisements