Reputation: 33
I am trying to add an active class to a list element and cannot work out why this method isn't working Any guidance will be greatly appreciated. Please see my code below.
Here's the HTML
<ul class="Menu">
<li class="tab" style=""><a href="javascript:void(0)" onclick="showContent(this,'tab1','tab1text')">Menu Item</a></li>
<li class="tab" style=""><a href="javascript:void(0)" onclick="showContent(this,'tab2','tab2text')">Menu item</a></li>
<li class="tab" style=""><a href="javascript:void(0)" onclick="showContent(this,'tab3','tab3text')">Menu Item</a></li>
<li class="tab" style=""><a href="javascript:void(0)" onclick="showContent(this,'tab4','tab4text')">Menu Item</a></li>
</ul>
And CSS with the classes
.tab {
color:;
background-color:;
font-family: 'Roboto', sans-serif;
}
.active {
color: #00ffff;
}
And the Javascript function
<script>
function showContent(obj, content, text)
obj.className += " active";
</script>
Here is a JSfiddle as well https://jsfiddle.net/wxjop98f/1/
I can't work out why this doesn't work as various tutorials state this method. Many thanks for any help provided.
Upvotes: 2
Views: 239
Reputation: 11
Just change your javascript as follows, you just missed curly braces
<script>
function showContent(obj, content, text)
{
obj.className = "active";
}
</script>
Upvotes: 1
Reputation: 62536
Several problems:
<head>
(no wrap){...}
obj.classList.add
(although obj.className += ' active'
will work too..active
to work on the <a>
elements you should use a.active
(otherwise the anchor
's color definition will get higher priority).Here is the fix:
https://jsfiddle.net/54w6ntx7/
And a complete snippet:
function showContent(obj, content, text) {
//debugger;
obj.classList.add("active");
}
a:link{
color: inherit;
text-decoration: inherit;
}
a:visited{
color: inherit;
}
a:active{
color: inherit;
}
.tab {
color:;
background-color:;
font-family: 'Roboto', sans-serif;
}
a.active {
color: green;
}
<ul class="Menu">
<li class="tab" style=""><a href="javascript:void(0)" onclick="showContent(this,'tab1','tab1text')">Menu Item</a></li>
<li class="tab" style=""><a href="javascript:void(0)" onclick="showContent(this,'tab2','tab2text')">Menu item</a></li>
<li class="tab" style=""><a href="javascript:void(0)" onclick="showContent(this,'tab3','tab3text')">Menu Item</a></li>
<li class="tab" style=""><a href="javascript:void(0)" onclick="showContent(this,'tab4','tab4text')">Menu Item</a></li>
</ul>
Upvotes: 6
Reputation: 534
You have put onclick function in anchor tag <a>
and you are putting active class on <a>
so please put onclick function on <li>
tag instead of <a>
.
Upvotes: 0
Reputation: 8668
You are missing the {} for your function:
function showContent(obj, content, text){
obj.className += " active";
}
https://jsfiddle.net/BradChelly/wxjop98f/4/
Upvotes: 1
Reputation: 2944
function showContent(obj, content, text) {
if (obj.className.indexOf("active") < 0)
obj.className += " active";
}
.tab {
color: ;
background-color: ;
font-family: 'Roboto', sans-serif;
}
.active {
color: #00ffff;
}
<ul class="Menu">
<li class="tab" style=""><a href="javascript:void(0)" onclick="showContent(this,'tab1','tab1text')">Menu Item</a>
</li>
<li class="tab" style=""><a href="javascript:void(0)" onclick="showContent(this,'tab2','tab2text')">Menu item</a>
</li>
<li class="tab" style=""><a href="javascript:void(0)" onclick="showContent(this,'tab3','tab3text')">Menu Item</a>
</li>
<li class="tab" style=""><a href="javascript:void(0)" onclick="showContent(this,'tab4','tab4text')">Menu Item</a>
</li>
</ul>
Upvotes: 0