In: Computer Science
How to place the same image in 4 separate divs by pressing a button using a javascript function?
HTML AND CSS:
<html>
<head>
<meta charset="utf-8">
<style>
body {
margin: 0;
}
#flex-container {
display: flex;
justify-content: space-between;
height: 300px;
}
nav {
background-color: royalblue;
height: 75px;
text-align:center;
}
#button {
}
header {
background-color: lightskyblue;
height: 400px;
}
section {
background-color: gray;
margin: 75px 100px;
/*height:650px;*/
padding-bottom:50px;
}
footer {
background-color: black;
height: 100px;
}
p {
background-color: khaki;
height: 200px;
margin-bottom: 100px;
}
div.col {
background-color: tomato;
border: 2px solid black;
width: calc(20%);
}
</style>
</head>
<title>Squarespace layout</title>
<body>
</head>
<body>
<nav>
<div class="button">
<button onclick = "addTheImage()">
click here for gorilla
</button>
</div>
</nav>
<header></header>
<section>
<!-- main paragraph -->
<p></p>
<!-- Two column-type things -->
<div id="flex-container">
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
</div>
</section>
<footer></footer>
</body>
</html>
MY JAVA CODE SO FAR: (THIS PUTS A NEW ONE AT THE BOTTOM EVERYTIME I NEED 1 EACH IN THE TOMATO COLORED DIV)
function addTheImage() {
var img = document.createElement('img');
img.src =
"https://media.giphy.com/media/27ppQUOxe7KlG/giphy.gif";
document.body.appendChild(img);
}
HTML page :
<html>
<head>
<meta charset="utf-8">
<style>
body {
margin: 0;
}
#flex-container {
display: flex;
justify-content: space-between;
height: 300px;
}
nav {
background-color: royalblue;
height: 75px;
text-align: center;
}
#button {}
header {
background-color: lightskyblue;
height: 400px;
}
section {
background-color: gray;
margin: 75px 100px;
/*height:650px;*/
padding-bottom: 50px;
}
footer {
background-color: black;
height: 100px;
}
p {
background-color: khaki;
height: 200px;
margin-bottom: 100px;
}
div.col {
background-color: tomato;
border: 2px solid black;
width: calc(20%);
}
img {
height: 150px;
width: 150px;
}
</style>
</head>
<title>Squarespace layout</title>
<body>
</head>
<body>
<nav>
<div class="button">
<button onclick="addTheImage()">
click here for gorilla
</button>
</div>
</nav>
<header></header>
<section>
<!-- main paragraph -->
<p></p>
<!-- Two column-type things -->
<div id="flex-container">
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
</div>
</section>
<footer></footer>
<script>
var i = 0;//declaring variable i
//function addTheImage
function addTheImage() {
var img = document.createElement('img');//creating img element
//assigning src to the image
img.src = "https://media.giphy.com/media/27ppQUOxe7KlG/giphy.gif";
//getting div
document.getElementsByClassName("col")[i].appendChild(img);
i++;//increment value of i
}
</script>
</body>
</html>
====================================
Screen when button is clicked for first time :
Screen when button is clicked for third time :