How can I make the image appear in the container Div?


username

In simple terms, I have a javascript code that generates random images when the user clicks a button. It's a blank page with a button and a container div. The problem is that the image appears outside the container, directly before. and I need to control where it is displayed. I want to put it in a container. I'm not a JavaScript expert and I found the code after a long search. I need help, thanks :)

The code is 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);
}
Hansen

You are appending the image to the body not the div. get the div var targetContainer = document.getElementsByClassName("container");then appendtargetContainer[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 -->

Related


How can I make the table appear as an image?

Pallavi Sharma I am trying to create a table or grid view in ionic framework. I was able to make the table but it doesn't look like what I need, check the table view in the image. Here is my code. In fact, I also have the header "Invoice#" and "account Name",

How can I make the table appear as an image?

Pallavi Sharma I am trying to create a table or grid view in ionic framework. I was able to make the table but it doesn't look like what I need, check the table view in the image. Here is my code. In fact, I also have the header "Invoice#" and "account Name",

How can I make the image appear and disappear randomly?

Christo Hardmn I have a problem with my mole images actually appearing but not disappearing. I just want one mole to appear and disappear after 10 seconds, another mole to appear and so on. This is my code: If I do this, time.delay(10)my code crashes. I've tri

How can I make my "logo" image appear in the header?

chpr486 I'm new to HTML and CSS, so sorry! I'm trying to add an image to the header to go to the left and above of the nav. Any help anyone can offer would be amazing! I've tried two ways to add an image, the first works, but it doesn't show (when I'm in the c

How can I make my "logo" image appear in the header?

chpr486 I'm new to HTML and CSS, so sorry! I'm trying to add an image to the header to go to the left and above of the nav. Any help anyone can offer would be amazing! I've tried two ways to add an image, the first works, but it doesn't show (when I'm in the c

How can I make my "logo" image appear in the header?

chpr486 I'm new to HTML and CSS, so sorry! I'm trying to add an image to the header to go to the left and above of the nav. Any help anyone can offer would be amazing! I've tried two ways to add an image, the first works, but it doesn't show (when I'm in the c

How can I make an image appear in HTML given certain conditions?

asg16 I am trying to display an image when the value of the slider is at a given position. (This is for a pH meter, so for a given pH I need a specific image) I'm relatively new to HTML and this is what I've come up with so far. The image of the fish that has

How can I make the text appear above the image?

Matt Brown Here is my website : http://www.matthewtbrown.com/newsite/index.html I want to hover over the image and move its type to 10px from the top and left. I don't want that type to appear before the transition. I'm referring to the type I want to use on e

How can I make my "logo" image appear in the header?

chpr486 I'm new to HTML and CSS, so sorry! I'm trying to add an image to the header to go to the left and above of the nav. Any help anyone can offer would be amazing! I've tried two ways to add an image, the first works, but it doesn't show (when I'm in the c

How can I make an image appear in HTML given certain conditions?

asg16 I am trying to display an image when the value of the slider is at a given position. (This is for a pH meter, so for a given pH I need a specific image) I'm relatively new to HTML and this is what I've come up with so far. The image of the fish that has

How can I make an image appear in HTML given certain conditions?

asg16 I am trying to display an image when the value of the slider is at a given position. (This is for a pH meter, so for a given pH I need a specific image) I'm relatively new to HTML and this is what I've come up with so far. The image of the fish that has

How can I make the border appear on the image when tabulating?

dark blue M I am building my first site. A big part of the specification is that it should be very user friendly. I have some images with hyperlinks at the top of my homepage. They have grey borders that turn pink when hovering over the image. My problem is th

How can I make the border appear on the image when tabulating?

dark blue M I am building my first site. A big part of the specification is that it should be very user friendly. I have some images with hyperlinks at the top of my homepage. They have grey borders that turn pink when you hover over the image. My problem is t

How can I make my "logo" image appear in the header?

chpr486 I'm new to HTML and CSS, so sorry! I'm trying to add an image to the header to go to the left and above of the nav. Any help anyone can offer would be amazing! I've tried two ways to add an image, the first works, but it doesn't show (when I'm in the c

How can I make my "logo" image appear in the header?

chpr486 I'm new to HTML and CSS, so sorry! I'm trying to add an image to the header to go to the left and above of the nav. Any help anyone can offer would be amazing! I've tried two ways to add an image, the first works, but it doesn't show (when I'm in the c

How can I make an image appear in HTML given certain conditions?

asg16 I am trying to display an image when the value of the slider is at a given position. (This is for a pH meter, so for a given pH I need a specific image) I'm relatively new to HTML and this is what I've come up with so far. The image of the fish that has

How can I make the image appear and disappear randomly?

Christo Hardmn I have a problem with my mole images actually appearing but not disappearing. I just want one mole to appear and disappear after 10 seconds, another mole to appear and so on. This is my code: If I do this, time.delay(10)my code crashes. I've tri

How can I make an image appear in HTML given certain conditions?

asg16 I am trying to display an image when the value of the slider is at a given position. (This is for a pH meter, so for a given pH I need a specific image) I'm relatively new to HTML and this is what I've come up with so far. The image of the fish that has

How can I make an image appear in HTML given certain conditions?

asg16 I am trying to display an image when the value of the slider is at a given position. (This is for a pH meter, so for a given pH I need a specific image) I'm relatively new to HTML and this is what I've come up with so far. The image of the fish that has

How can I make an image appear in HTML given certain conditions?

asg16 I am trying to display an image when the value of the slider is at a given position. (This is for a pH meter, so for a given pH I need a specific image) I'm relatively new to HTML and this is what I've come up with so far. The image of the fish that has

How can I make the text appear above the image?

Matt Brown Here is my website : http://www.matthewtbrown.com/newsite/index.html I want to hover over the image and move its type to 10px from the top and left. I don't want that type to appear before the transition. I'm referring to the type I want to use on e

How can I make the border appear on the image when tabulating?

dark blue M I am building my first site. A big part of the specification is that it should be very user friendly. I have some images with hyperlinks at the top of my homepage. They have grey borders that turn pink when hovering over the image. My problem is th

How can I make my "logo" image appear in the header?

chpr486 I'm new to HTML and CSS, so sorry! I'm trying to add an image to the header to go to the left and above of the nav. Any help anyone can offer would be amazing! I've tried two ways to add an image, the first works, but it doesn't show (when I'm in the c