user3047469
user3047469

Reputation: 39

How to make the image shown inside the container Div?

Simply I've a javascript code which generate a random image when the user click the button. it's an empty page with one button and a container div. the problem is the image appear outside the container, it appear directly before . and I need to control where it will appear. I want it inside the container. I'm not a javascript expert I just found the code after long search. I need help, Thank you :)

Code here: https://codepen.io/Haitham1000/pen/aXjYzz

function display_random_image() 
{
var theImages = [{
src: "http://farm4.staticflickr.com/3691/11268502654_f28f05966c_m.jpg",
width: "240",
height: "160"
}, {
src: "http://farm1.staticflickr.com/33/45336904_1aef569b30_n.jpg",
width: "320",
height: "195"
}, {
src: "http://farm6.staticflickr.com/5211/5384592886_80a512e2c9.jpg",
width: "500",
height: "343"
}];

var preBuffer = [];
for (var i = 0, j = theImages.length; i < j; i++) {
preBuffer[i] = new Image();
preBuffer[i].src = theImages[i].src;
preBuffer[i].width = theImages[i].width;
preBuffer[i].height = theImages[i].height;
}

// create random image number
function getRandomInt(min,max) 
{
//  return Math.floor(Math.random() * (max - min + 1)) + min;

imn = Math.floor(Math.random() * (max - min + 1)) + min;
return preBuffer[imn];
}  

// 0 is first image,   preBuffer.length - 1) is  last image

var newImage = getRandomInt(0, preBuffer.length - 1);

// remove the previous images
var images = document.getElementsByTagName('img');
var l = images.length;
for (var p = 0; p < l; p++) {
images[0].parentNode.removeChild(images[0]);
}
// display the image  
var targetContainer = document.getElementsByClassName("container");
targetContainer[0].appendChild(newImage);
}

Upvotes: 1

Views: 86

Answers (1)

KHansen
KHansen

Reputation: 930

You are appending the image to the body instead of the div. Get the div var targetContainer = document.getElementsByClassName("container"); and then append it targetContainer[0].appendChild(newImage);

function display_random_image() 
{
     var theImages = [{
        src: "http://farm4.staticflickr.com/3691/11268502654_f28f05966c_m.jpg",
        width: "240",
        height: "160"
    }, {
        src: "http://farm1.staticflickr.com/33/45336904_1aef569b30_n.jpg",
        width: "320",
        height: "195"
    }, {
        src: "http://farm6.staticflickr.com/5211/5384592886_80a512e2c9.jpg",
        width: "500",
        height: "343"
    }];
    
    var preBuffer = [];
    for (var i = 0, j = theImages.length; i < j; i++) {
        preBuffer[i] = new Image();
        preBuffer[i].src = theImages[i].src;
        preBuffer[i].width = theImages[i].width;
        preBuffer[i].height = theImages[i].height;
    }
   
// create random image number
  function getRandomInt(min,max) 
    {
      //  return Math.floor(Math.random() * (max - min + 1)) + min;
    
imn = Math.floor(Math.random() * (max - min + 1)) + min;
    return preBuffer[imn];
    }  

// 0 is first image,   preBuffer.length - 1) is  last image
  
var newImage = getRandomInt(0, preBuffer.length - 1);
 
// remove the previous images
var images = document.getElementsByTagName('img');
var l = images.length;
for (var p = 0; p < l; p++) {
    images[0].parentNode.removeChild(images[0]);
}
// display the image  
 var targetContainer = document.getElementsByClassName("container");
targetContainer[0].appendChild(newImage);
}
body {margin-top: 30px;}
<div class="container">
<button id="jsstyle" 
onclick="display_random_image();">Show Image</button> 
</div> <!-- Container -->

Upvotes: 1

Related Questions