I have 2 div elements, and I want to add something on the Div 2 that when I Click on that Div, it will Display Div 1, so both DIV’s will be visible when clicking Div 2! Div 2 should remain visible all the time, but when I click on DIV 2, it should display Div 1!
I want a JS script!
JavaScript
x
63
63
1
.box1 {
2
display: none;
3
color: white;
4
font-weight: 100;
5
background-color: rgba(234, 21, 56, 0.3);
6
font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
7
border: 3px solid #ea1538;
8
width: 200px;
9
max-height: 300px;
10
overflow: scroll;
11
margin-left: 20px;
12
margin-top: 40px;
13
transition: ease 0.5s;
14
padding: 10px 10px;
15
box-sizing: border-box;
16
box-shadow: 0px 0px 10px gainsboro;
17
}
18
19
.box1::-webkit-scrollbar {
20
display: none;
21
}
22
23
.box1:hover {
24
transition: ease 0.5s;
25
transform: scale(1.1);
26
}
27
28
#cover1 {
29
width: 100%;
30
height: 40%;
31
}
32
33
.box2 {
34
display: inline-block;
35
color: white;
36
font-weight: 100;
37
background-color: rgba(234, 21, 56, 0.3);
38
font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
39
border: 3px solid #ea1538;
40
width: 200px;
41
max-height: 300px;
42
overflow: scroll;
43
margin-left: 1%;
44
margin-top: 40px;
45
transition: ease 0.5s;
46
padding: 10px 10px;
47
box-sizing: border-box;
48
box-shadow: 0px 0px 10px gainsboro;
49
}
50
51
.box2::-webkit-scrollbar {
52
display: none;
53
}
54
55
.box2:hover {
56
transition: ease 0.5s;
57
transform: scale(1.1);
58
}
59
60
#cover2 {
61
width: 100%;
62
height: 40%;
63
}
JavaScript
1
21
21
1
<div class="box1">
2
<img src="/images/Books/book_bg.jpg" alt="cover" id="cover1" />
3
<h3 class="desc1">
4
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Et iste ipsum harum beatae dolor fugit repudiandae a quaerat doloremque pariatur, suscipit molestias ipsa minus. Non explicabo quam quasi illo accusamus aliquid, reiciendis autem? Quas odio hic
5
pariatur necessitatibus nobis nisi fugiat ab voluptate. Perferendis maiores quisquam cumque quod aspernatur ipsa?
6
</h3>
7
</div>
8
9
10
<div class="box2">
11
<button type="button" id="box2btn" onclick="href='window.location.https://www.facebook.com'">
12
<img src="/images/Books/book_bg.jpg" alt="cover" id="cover2" />
13
<h3>
14
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Excepturi
15
natus quidem perferendis architecto sapiente, eius, praesentium, odio
16
illo provident quos nostrum quaerat! Placeat saepe, blanditiis id
17
assumenda ab autem in unde maxime alias, obcaecati distinctio expedita
18
veritatis deserunt atque exercitationem quasi dolorum eum quas. Voluptas
19
consequatur nisi sint porro quos?
20
</h3>
21
</div>
Advertisement
Answer
Here we go https://jsfiddle.net/byr0kqL5/
The simple function
JavaScript
1
5
1
function addNewDiv() {
2
const box1 = document.getElementById("box1-content") //find element with id `box1-content`
3
box1.classList.remove("display-none"); //make box 1 visible by `display-none` removal
4
}
5
but you need to add this style
JavaScript
1
4
1
.display-none {
2
display: none;
3
}
4
Your box 1 will be like this
JavaScript
1
2
1
<div class="box1 display-none" id="box1-content"></div>
2
Lastly, your box 2 needs to have onclick
event
JavaScript
1
2
1
<div class="box2" onclick="addNewDiv()"></div>
2