Blue Line
Blue Line

Reputation: 81

The image is loading correctly, but it fails to change when using javascript function

<img id="myImg" src="https://drive.google.com/uc?export=view&amp;id=1gsRqRNoKabIkK9z1NhioUhpFOgIKww5Q" width="107" height="98">

<button onclick="change_image()">Change Image</button>
<script>
function change_image() {
  document.getElementById("myImg").src = "https://drive.google.com/uc?export=view&amp;id=1gsRqRNoKabIkK9z1NhioUhpFOgIKww5Q";
}
</script>

Image from the same link is loading properly. But it fails to change when using a js function

Upvotes: 1

Views: 51

Answers (1)

Quentin
Quentin

Reputation: 943561

&amp; is how you represent a & character in HTML.

It isn't how you represent one in JavaScript (not even JS in a <script> element in an HTML document as <script> elements are defined so that normal HTML parsing rules (where & would switch to the character reference state) are suspended).

The URL is getting a 400 error.

In the JS use only & without the amp;.

Upvotes: 2

Related Questions