1

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

Clyde Lobo
  • 9,126
  • 7
  • 34
  • 61

5 Answers5

5

js does not have an inbuilt add and remove class

Try this

To add a class

document.getElementById("MyElement").className += " MyClass";

To remove a class

document.getElementById("MyElement").className = document.getElementById("MyElement").className.replace ( /(?:^|\s)MyClass(?!\S)/ , '' )

And if you need to check if an element has a class

function hasClass(ele,cls) {
return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
Clyde Lobo
  • 9,126
  • 7
  • 34
  • 61
3

There's no addClass / removeClass in vanilla JavaScript.

Either you have to use HTML5 classList API or directly manipulate v.className.

Samuli Hakoniemi
  • 18,740
  • 1
  • 61
  • 74
1
document.getElementById("idElement").setAttribute("class", "className");
Anooj P
  • 346
  • 4
  • 16
1

Tried to do it with less code. Hoefully it does what you need it to do.

HTML:

<ul>
<li><a class="aSwitch" href="#" >Click something</a>
<ul id="item1" style="display:none">
    <li>Something ...</li>
    <li>Something something</li>
</ul></li>
<li><a class="aSwitch" href="#" >Click something else</a>
<ul id="item2" style="display:none">
    <li>Something more...</li>
    <li>Something something less?</li>
</ul></li>

CSS:

a:link {
color: #000000;
text-decoration: underline;
}

a.selected {
color: #005b97;
text-decoration: none;
padding-left: 2px;
}

jQuery

$('a.aSwitch').click(function() {
$(this).next().toggle();
$(this).toggleClass('selected');
});

See it working here: FIDDLE

effectica
  • 780
  • 6
  • 12
0

Most easily u can add jquery addon

<script src="../../Scripts/jquery-ui-1.8.11.min.js" type="text/javascript"></script>

Than change your code to

<script type="text/javascript">
function toggle(id) {
    var v = document.getElementById(id);
    $('#' + id).toggleClass("selected");
    if (v.style.display == '') {
        v.style.display = 'none';
    } else {
        v.style.display = '';
    }
}
</script>
Berker Yüceer
  • 7,026
  • 18
  • 68
  • 102