0

When I set the iframe source using javascript it doesn't work. The first alert message shows up as the data-src of the iframe, but the second shows up empty.

for (var i = 0; i < images.length; i++) {
  var slide = images.item(i);

  slide.style.src = slide.dataset.src;
  alert(slide.dataset.src);

  alert(slide.style.src);
}
<iframe class="load color light" data-type="src" width="865" height="572" data-src="https://www.youtube.com/embed/ONA67lhVpLs?rel=0" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

1
  • 7
    style changes the stylesheet of the element. Use slide.src instead Commented Aug 7, 2020 at 19:16

2 Answers 2

2

Use slide.src instead of slide.style.src.

You want to set the HTML attribute element.src, element.style.src sets a CSS property src, which isn't setting the iframe src. Read more about the CSS property src here.

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

Comments

2

The style property is for changing CSS properties. src is an HTML attribute.

Try using this :

for (var i = 0; i < images.length; i++) {
  let slide = images.item(i);
  slide.setAttribute("src", slide.dataset.src);
  alert(slide.dataset.src);

  alert(slide.getAttribute("src"));
}

Comments

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.