May I know how to solve my problem, how will it work if you click the button then the confirm button then the text “Hello” will change to a “Hi” word? I tried my best to solve it but I still can not know how to figure my problem that is why I am asking for a help. https://jsfiddle.net/chrismontage/dverj816/10/
JavaScript
x
26
26
1
<button class = "btn btn1" id = "myBtn">Button</button>
2
3
<h4 id = "message">
4
Hello
5
</h4>
6
7
<!-- The Modal -->
8
<div id="myModal" class="modal">
9
10
<!-- Modal content -->
11
<div class="modal-content">
12
<div class="modal-header">
13
14
<span class="close">×</span>
15
</div>
16
<div class="modal-body">
17
<h4 class = "text-center">Are you sure you want to cancel your order?</h4>
18
</div>
19
<div class="modal-footer mx-auto">
20
<button class = "btn" id = "confirm" onclick = "changeStatus()">Confirm</button>
21
<button class = "btn" id = "cancel">Cancel</button>
22
</div>
23
</div>
24
25
</div>
26
JavaScript
1
57
57
1
/* The Modal (background) */
2
.modal {
3
display: none; /* Hidden by default */
4
position: fixed; /* Stay in place */
5
z-index: 1; /* Sit on top */
6
padding-top: 100px; /* Location of the box */
7
left: 0;
8
top: 0;
9
width: 100%; /* Full width */
10
height: 100%; /* Full height */
11
overflow: auto; /* Enable scroll if needed */
12
background-color: rgb(0,0,0); /* Fallback color */
13
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
14
}
15
16
/* Modal Content */
17
.modal-content {
18
position: relative;
19
background-color: #fefefe;
20
margin: auto;
21
padding: 0;
22
border: 1px solid #888;
23
max-width: 40%;
24
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
25
-webkit-animation-name: animatetop;
26
-webkit-animation-duration: 0.4s;
27
animation-name: animatetop;
28
animation-duration: 0.4s
29
}
30
31
/* Add Animation */
32
@-webkit-keyframes animatetop {
33
from {top:-300px; opacity:0}
34
to {top:0; opacity:1}
35
}
36
37
@keyframes animatetop {
38
from {top:-300px; opacity:0}
39
to {top:0; opacity:1}
40
}
41
42
/* The Close Button */
43
.close {
44
color: white;
45
float: right;
46
font-size: 28px;
47
font-weight: bold;
48
}
49
50
.close:hover,
51
.close:focus {
52
color: $color5;
53
text-decoration: none;
54
cursor: pointer;
55
}
56
57
JavaScript
1
47
47
1
// Get the modal
2
var modal = document.getElementById("myModal");
3
4
// Get the button that opens the modal
5
var btn1 = document.getElementById("myBtn");
6
7
var confirm = document.getElementById("confirm");
8
9
var cancel = document.getElementById("cancel");
10
11
// Get the <span> element that closes the modal
12
var span = document.getElementsByClassName("close")[0];
13
14
// When the user clicks the button, open the modal
15
btn1.onclick = function() {
16
modal.style.display = "block";
17
}
18
19
confirm.onclick = function () {
20
modal.style.display = "none";
21
}
22
23
function changeStatus() {
24
document.getElementById("message").innerHTML = "Hi";
25
}
26
27
cancel.onclick = function () {
28
modal.style.display = "none";
29
}
30
// When the user clicks on <span> (x), close the modal
31
span.onclick = function() {
32
modal.style.display = "none";
33
}
34
35
// When the user clicks anywhere outside of the modal, close it
36
window.onclick = function(event) {
37
if (event.target == modal) {
38
modal.style.display = "none";
39
}
40
}
41
42
43
44
45
46
47
Advertisement
Answer
Just add one line to your confirm.onclick
function:
JavaScript
1
5
1
confirm.onclick = function () {
2
modal.style.display = "none";
3
changeStatus();
4
}
5
You had the function right, just probably forgot to add it to your onclick listener.