I have been browsing here and looking at similar problems, however none of the solutions seem to work. This is also my first posted question.
JavaScript
x
29
29
1
<table>
2
<tr class="foo" onclick="openEmail1();"></tr>
3
<tr class="foo" onclick="openEmail2();"></tr>
4
<tr class="foo" onclick="openEmail3();"> <!-- this opens fine-->
5
<td>From: Me</td>
6
<td>Subject: Why won't this work?</td>
7
<td>Date:
8
9
<div style="display: none" id="email3">..email popup..
10
11
<div>header box in email popup</div>
12
<div>email body box in email popup</div>
13
<div>
14
<button onclick="openForm();">Forward</button> <!-- this works fine-->
15
<button onclick="closeEmail3();">Close</button> <!-- does not work-->
16
</div>
17
18
</div>
19
<script>
20
function openEmail3(){
21
document.getElementById("email3").style.display = "block";
22
}
23
function closeEmail3(){
24
document.getElementById("email3").style.display = "none";
25
}
26
</script>
27
</td>
28
</tr>
29
</table>
Advertisement
Answer
Your code isn’t working because of event bubbling
I’ve updated your code to be more legible by putting your element in a var
JavaScript
1
2
1
var div = document.getElementById("email3");
2
Changing your button element to this will stop the default functionality
JavaScript
1
2
1
<button onclick="closeEmail3();event.stopPropagation()">Close</button>
2
Run the snippet below to see it in action.
JavaScript
1
15
15
1
var div = document.getElementById("email3");
2
3
function openEmail3(){
4
if (div.style.display === "none") {
5
div.style.display = "block";
6
}
7
else {
8
div.style.display === "none" }
9
}
10
11
function closeEmail3(){
12
if (div.style.display === "block") {
13
div.style.display = "none";
14
}
15
}
JavaScript
1
20
20
1
<table>
2
<tr class="foo" onclick="openEmail1();"></tr>
3
<tr class="foo" onclick="openEmail3();"> <!-- this opens fine-->
4
<td>From: Me</td>
5
<td>Subject: Why won't this work?</td>
6
<td>Date:
7
8
<div style="display: none" id="email3">..email popup..
9
10
<div>header box in email popup</div>
11
<div>email body box in email popup</div>
12
<div>
13
<button onclick="openForm();">Forward</button> <!-- this works fine-->
14
<button onclick="closeEmail3();event.stopPropagation()">Close</button> <!-- does not work-->
15
</div>
16
17
</div>
18
</td>
19
</tr>
20
</table>