Skip to main content
Participant
August 18, 2021
Answered

How to change an image with javscript?

  • August 18, 2021
  • 3 replies
  • 254 views

Hi.
I need to display an image from an external url. For example, if I click on component 1, I get the image of component 1 and if I then click on component 2, it changes the source of the image and shows the image of component 2.
I'm fetching the info from a database and I can load all the texts without problem when I change the component, but I don't know what to do with the image.
Thank you very much in advance for the collaboration.

    This topic is closed to new replies. Start a new post to keep the conversation going.
    Correct answer kglad

    create an image object and change its src per your needs:

     

    var image = new Image();

    image.onload = onLoadF;
    image.src=whatever path/name;

     

    function onLoadF(e) {
    // Create a Bitmap from the loaded image
    var img = new createjs.Bitmap(e.target)
    stage.addChild(img);
    positionF(img);
    // Render Stage
    stage.update();
    }

    3 replies

    kglad
    Community Expert
    kgladCommunity ExpertCorrect answer
    Community Expert
    August 18, 2021

    create an image object and change its src per your needs:

     

    var image = new Image();

    image.onload = onLoadF;
    image.src=whatever path/name;

     

    function onLoadF(e) {
    // Create a Bitmap from the loaded image
    var img = new createjs.Bitmap(e.target)
    stage.addChild(img);
    positionF(img);
    // Render Stage
    stage.update();
    }

    Participant
    August 18, 2021

    Correct!
    Thank you very much.

    kglad
    Community Expert
    Community Expert
    August 18, 2021

    you're welcome.