Javascript examples for DOM HTML Element:Image
The width property sets or gets the width attribute of an image.
Set the width property with the following Values
Value | Description |
---|---|
pixels | The width in pixels (e.g. width="100") |
A Number, representing the width of the image, in pixels
The following code shows how to change the width of an image to 300px:
<!DOCTYPE html> <html> <body> <img id="myImg" src="http://java2s.com/resources/a.png" width="107" height="98"> <button onclick="myFunction()">Test</button> <script> function myFunction() {/*from w w w . j a va 2 s .c o m*/ document.getElementById("myImg").width = "300"; } </script> </body> </html>