I'm fairly new to Javascript, and I've run into an issue which is annoying.
I have a list containing lists. I have a script that sets the list visible / invisible on click. However, I wan't to toggle / add a class to the link, once it has been pressed.
My list looks like this
<ul>
<li><a href="#" onclick="toggle('item1');">Click something</a>
<ul id="item1" style="display: none;">
<li>Something ...</li>
<li>Something something</li>
</ul></li>
<li><a href="#" onclick="toggle('item2');">Click something else</a>
<ul id="item2" style="display: none;">
<li>Something more...</li>
<li>Something something less?</li>
</ul></li>
</ul>
and my script looks like this:
<script type="text/javascript">
function toggle(id) {
var v = document.getElementById(id);
if (v.style.display == '') {
v.style.display = 'none';
v.removeClass("selected");
} else {
v.style.display = '';
v.addClass("selected");
}
}
</script>
The list shows and hides as it's supposed to, but the class is not added nor removed.
CSS is like this:
a:link {
color: #000000;
text-decoration: none;
}
a:hover, a.selected {
color: #005b97;
text-decoration: none;
padding-left: 2px;
}
Thanks in advance
Best Regards Benjamin