how to add image in javascript code?

+1 Parul Bhagat · January 13, 2016
Suppose that I have a javascript code . I want that in the code if a condition is true then it displays an image and if the condition is false it should display another image . Then how should I do this?

Post a Reply

Replies

Oldest  Newest  Rating
0 Parul Bhagat · January 15, 2016
@Jagdeep Matharu
I am a new learner so can you please explain the function show_image ,its attributes and the objects that you are initializing in the show_image function?
0 Eldar Bakerman · January 14, 2016
or, in HTML you could do:
<img src="http://i1.cdn2b.image.pornhub.phncdn.com/m=eWdT8daaaa/videos/201512/26/64649581/original/6.jpg">
+1 Jagdeep Matharu · January 13, 2016
:O:O:O

????? ??? ?

this looks so buggy. lol
+1 Jagdeep Matharu · January 13, 2016
There are many ways to do this depending on Your USE case,

here is one quickcode:

Javascript:
       function add_google_logo() {
            var foo = 0;
       
           
        if (foo == 0) {
                   var src = "http://google.com/images/logo.gif";
            show_image(src, 276,110, "Google Logo");
                } else if(foo==1){
                var src = "http://www.w3schools.com/images/w3schools.png";
            show_image(src, 276,110, "Google Logo");
                }
    }


    function show_image(src, width, height, alt) {
        var img = document.createElement("img");
        img.src = src;
        img.width = width;
        img.height = height;
        img.alt = alt;
        document.body.appendChild(img);
    }



HTML:
<button onclick="add_google_logo();">Click Me!</button>
  • 1

Javascript

107,228 followers
About

A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.

Links
Moderators