user3407899 user3407899 - 3 months ago 14
HTML Question

jQuery dynamically load image not working showing multipule images how to I replace the src= and not append to it

Okay so I have the little function below on my html page that receives data from another script using socket.io to update the url.

The problems is the image can change up to 10 times during the user is using my page.

If I am loading one image, it works, but multiple images don't work, as it appends the next picture to the

src=
.

And not replace it so I end up with multiple images in my div.

How do I changed it so it replace the
src=
and not append to it?

The
src=
is originally empty

Here's my jQuery script:

<script src="/js/jquery.js"></script>
<script>

function hellou(url){

var img = document.createElement("img");
img.src = url;

var src = document.getElementById("cpt_img");
src.appendChild(img);
}
</script>


Can someone show me an example to fix this?

Answer

use jquery>>

function hellou(url){

$("#cpt_img").attr("src",url);

}

can I have your HTML .. so that I can give you more specific answer