Search code examples
javascripthtmlimagesize

image size adjust (no IDs) by a single javascript


How to adjust size of all images to 400*300 in a html file keeping following conditions ?

  1. with a single javascript located at the top
  2. without any additional file like css
  3. there are no Ids for images at present

In other words, I am finding a javascript (blahblah part) of a form

<script>
...
blahblah
...
</script>

<img src="image/01.jpg">
<img src="image/02.jpg">
...
<img src="image/99.jpg">

which will get the same effect as following

<img src="image/01.jpg" width=400 height=300>
<img src="image/02.jpg" width=400 height=300>
...
<img src="image/99.jpg" width=400 height=300>

Solution

  • window.addEventListener('DOMContentLoaded', (event) => document.querySelectorAll('img').forEach((img) => ([img.width, img.height] = [400, 300])), false);