0

I have HTML like this:

<div class="h5p-image">
  <img src="localhost/images/file-5fa55a3d758c4.jpg" alt="New image" title="" style="width: 100%; height: 100%;">
</div>

and I want to select that image via src, so my JS looks like this:

var list= document.querySelectorAll('.h5p-image > img').src = "file-5fa55a3d758c4.jpg";
var i;
for (i = 0; i < list.length; i++) {
  list[i].style.width = "300px";
}

Error from the console:

Uncaught TypeError: Cannot read property 'length'

Can anybody try to help me with this?

0

1 Answer 1

1

Your list is either

var list= document.querySelectorAll('.h5p-image > img')

OR

var list= document.querySelectorAll('.h5p-image > img[src$="5fa55a3d758c4.jpg"]') (using ends-with)

assuming you have more than one div with that image.

So the code is likely this to style the div

[...document.querySelectorAll('.h5p-image > img[src$="5fa55a3d758c4.jpg"]')]
.forEach(img => img.closest("div").style.width = "300px");

or this to just style the image

[...document.querySelectorAll('.h5p-image > img[src$="5fa55a3d758c4.jpg"]')]
.forEach(img => img.style.width = "300px");

Or if there is only ONE image

document.querySelector('.h5p-image > img').style.width = "300px"

[...document.querySelectorAll('.h5p-image > img[src$="758c4.jpg"]')]
.forEach(img => img.style.width = "300px");
<div class="h5p-image">
  <img src="https://www.nice-premium.com/local/cache-vignettes/L600xH800/2012-03-19_08.53.13-758c4.jpg" alt="New image" title="" style="width: 100%; height: 100%;">
</div>


<div class="h5p-image">
  <img src="https://www.nice-premium.com/local/cache-vignettes/L600xH800/2012-03-19_08.53.30-f371c.jpg" alt="New image" title="" style="width: 100%; height: 100%;">
</div>


<div class="h5p-image">
  <img src="https://www.nice-premium.com/local/cache-vignettes/L600xH800/2012-03-19_08.53.13-758c4.jpg" alt="New image" title="" style="width: 100%; height: 100%;">
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I got this error from the console " Failed to execute 'querySelectorAll' on 'Document': '.h5p-image > img[src*=file-5fa55a3d758c4.jpg]' is not a valid selector."
Still, " Failed to execute 'querySelectorAll' on 'Document': '.h5p-image > img[src$=5fa55a3d758c4.jpg]' is not a valid selector."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.