I want to put tap function in other content too but it is not working.
when you click tap on the top, each tap display proper content.
in there i want to put more content, so i made #Hometwo, #Newstwo, #Contacttwo, #Abouttwo
so when i clicked Home tap then it show #Home and #Hometwo text.
but it only works on #Home. why is it not working on “#Hometwo”?
what should i do to make it work on “#Hometwo” either??
any help will be so appreciated. thanks!
JavaScript
x
14
14
1
function openPage(pageName,elmnt,color) {
2
var i, tabcontent, tablinks;
3
tabcontent = document.getElementsByClassName("tabcontent");
4
for (i = 0; i < tabcontent.length; i++) {
5
tabcontent[i].style.display = "none";
6
}
7
tablinks = document.getElementsByClassName("tablink");
8
9
document.getElementById(pageName).style.display = "block";
10
11
}
12
13
// Get the element with id="defaultOpen" and click on it
14
document.getElementById("defaultOpen").click();
JavaScript
1
33
33
1
* {box-sizing: border-box}
2
3
/* Set height of body and the document to 100% */
4
body, html {
5
height: 100%;
6
margin: 0;
7
font-family: Arial;
8
}
9
10
/* Style tab links */
11
.tablink {
12
background-color: #555;
13
color: white;
14
float: left;
15
border: none;
16
outline: none;
17
cursor: pointer;
18
padding: 14px 16px;
19
font-size: 17px;
20
width: 25%;
21
}
22
23
.tablink:hover {
24
background-color: #777;
25
}
26
27
/* Style the tab content (and add height:100% for full page content) */
28
.tabcontent {
29
color: black;
30
display: none;
31
padding: 100px 20px;
32
height: 100%;
33
}
JavaScript
1
65
65
1
<button class="tablink" onclick="openPage('Home', this, 'Hometwo')">Home</button>
2
<button class="tablink" onclick="openPage('News', this, 'Newstwo')" id="defaultOpen">News</button>
3
<button class="tablink" onclick="openPage('Contact', this, 'Contacttwo')">Contact</button>
4
<button class="tablink" onclick="openPage('About', this, 'Abouttwo')">About</button>
5
6
<table>
7
<tr>
8
<td id="Home" class="tabcontent">
9
<div>
10
<h3>Home</h3>
11
<p>Home is where the heart is..</p>
12
</div>
13
</td>
14
<td id="News" class="tabcontent">
15
<div>
16
<h3>News</h3>
17
<p>Home is where the heart is..</p>
18
</div>
19
</td>
20
<td id="Contact" class="tabcontent">
21
<div>
22
<h3>Contact</h3>
23
<p>Home is where the heart is..</p>
24
</div>
25
</td>
26
<td id="About" class="tabcontent">
27
<div>
28
<h3>About</h3>
29
<p>Home is where the heart is..</p>
30
</div>
31
</td>
32
</tr>
33
34
<tr>
35
<td>this is text which always have to be displayed</td>
36
</tr>
37
38
<tr>
39
<td id="Hometwo" class="tabcontent">
40
<div>
41
<h3>Home2</h3>
42
<p>Home is where the heart is..</p>
43
</div>
44
</td>
45
<td id="Newstwo" class="tabcontent">
46
<div>
47
<h3>News2</h3>
48
<p>Home is where the heart is..</p>
49
</div>
50
</td>
51
<td id="Contacttwo" class="tabcontent">
52
<div>
53
<h3>Contact2</h3>
54
<p>Home is where the heart is..</p>
55
</div>
56
</td>
57
<td id="Abouttwo" class="tabcontent">
58
<div>
59
<h3>About2</h3>
60
<p>Home is where the heart is..</p>
61
</div>
62
</td>
63
</tr>
64
65
</table>
Advertisement
Answer
First your openPage
function accepts three params, the third being the color:
JavaScript
1
2
1
function openPage(pageName,elmnt,color) {
2
But you’re passing 'Hometwo'
for your third params on the clickevent. So either change that, or add a string to the display in your openPage
function.
Here’s a working sample:
JavaScript
1
18
18
1
function openPage(pageName,elmnt,pageName2) {
2
var i, tabcontent, tablinks;
3
tabcontent = document.getElementsByClassName("tabcontent");
4
for (i = 0; i < tabcontent.length; i++) {
5
tabcontent[i].style.display = "none";
6
}
7
tablinks = document.getElementsByClassName("tablink");
8
for (i = 0; i < tablinks.length; i++) {
9
tablinks[i].style.backgroundColor = "";
10
}
11
document.getElementById(pageName).style.display = "block";
12
elmnt.style.backgroundColor = "";
13
document.getElementById(pageName2).style.display = "block";
14
elmnt.style.backgroundColor = "";
15
}
16
17
// Get the element with id="defaultOpen" and click on it
18
document.getElementById("defaultOpen").click();
JavaScript
1
33
33
1
* {box-sizing: border-box}
2
3
/* Set height of body and the document to 100% */
4
body, html {
5
height: 100%;
6
margin: 0;
7
font-family: Arial;
8
}
9
10
/* Style tab links */
11
.tablink {
12
background-color: #555;
13
color: white;
14
float: left;
15
border: none;
16
outline: none;
17
cursor: pointer;
18
padding: 14px 16px;
19
font-size: 17px;
20
width: 25%;
21
}
22
23
.tablink:hover {
24
background-color: #777;
25
}
26
27
/* Style the tab content (and add height:100% for full page content) */
28
.tabcontent {
29
color: black;
30
display: none;
31
padding: 100px 20px;
32
height: 100%;
33
}
JavaScript
1
65
65
1
<button class="tablink" onclick="openPage('Home', this, 'Hometwo')">Home</button>
2
<button class="tablink" onclick="openPage('News', this, 'Newstwo')" id="defaultOpen">News</button>
3
<button class="tablink" onclick="openPage('Contact', this, 'Contacttwo')">Contact</button>
4
<button class="tablink" onclick="openPage('About', this, 'Abouttwo')">About</button>
5
6
<table>
7
<tr>
8
<td id="Home" class="tabcontent">
9
<div>
10
<h3>Home</h3>
11
<p>Home is where the heart is..</p>
12
</div>
13
</td>
14
<td id="News" class="tabcontent">
15
<div>
16
<h3>News</h3>
17
<p>Home is where the heart is..</p>
18
</div>
19
</td>
20
<td id="Contact" class="tabcontent">
21
<div>
22
<h3>Contact</h3>
23
<p>Home is where the heart is..</p>
24
</div>
25
</td>
26
<td id="About" class="tabcontent">
27
<div>
28
<h3>About</h3>
29
<p>Home is where the heart is..</p>
30
</div>
31
</td>
32
</tr>
33
34
<tr>
35
<td>this is text which always have to be displayed</td>
36
</tr>
37
38
<tr>
39
<td id="Hometwo" class="tabcontent">
40
<div>
41
<h3>Home2</h3>
42
<p>Home is where the heart is..</p>
43
</div>
44
</td>
45
<td id="Newstwo" class="tabcontent">
46
<div>
47
<h3>News2</h3>
48
<p>Home is where the heart is..</p>
49
</div>
50
</td>
51
<td id="Contacttwo" class="tabcontent">
52
<div>
53
<h3>Contact2</h3>
54
<p>Home is where the heart is..</p>
55
</div>
56
</td>
57
<td id="Abouttwo" class="tabcontent">
58
<div>
59
<h3>About2</h3>
60
<p>Home is where the heart is..</p>
61
</div>
62
</td>
63
</tr>
64
65
</table>