Skip to content

Commit dc7fb6f

Browse files
committed
add first working solution of tabs
1 parent 29478e6 commit dc7fb6f

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

chapter_fifteen_tabs/index.html

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<meta name="viewport" content="width=device-width, initial-scale=1.0">
@@ -11,16 +12,19 @@
1112
}
1213
</style>
1314
</head>
15+
1416
<body>
1517
<tab-panel>
1618
<div data-tabname="one">Tab one</div>
1719
<div data-tabname="two">Tab two</div>
1820
<div data-tabname="three">Tab three</div>
1921
</tab-panel>
2022
<script>
21-
23+
2224
function asTabs(node) {
2325
const children = Array.from(node.children);
26+
const header = document.createElement('header');
27+
document.body.insertBefore(header, document.querySelector('tab-panel'));
2428

2529
function manageSelection(event) {
2630
if (document.querySelector('.selected')) {
@@ -33,7 +37,7 @@
3337
if (childNode.style.display === 'block') {
3438
childNode.style.display = 'none';
3539
}
36-
if (childNode.getAttribute('data-tabname') === event.target.textContent ) {
40+
if (childNode.getAttribute('data-tabname') === event.target.textContent) {
3741
childNode.style.display = 'block'
3842
}
3943
})
@@ -43,12 +47,13 @@
4347
const button = document.createElement('button');
4448
button.textContent = childNode.getAttribute('data-tabname');
4549
button.addEventListener('click', manageSelection)
46-
document.body.appendChild(button);
50+
document.querySelector('header').appendChild(button);
4751
childNode.style.display = 'none';
4852
})
4953

5054
}
5155
asTabs(document.querySelector("tab-panel"));
5256
</script>
5357
</body>
58+
5459
</html>

0 commit comments

Comments
 (0)