Hejhejhej123
Hejhejhej123

Reputation: 1195

Change css with another button and class name

I am trying to remove some links inside different span tags with the same class (.front) when a button is pressed (class .tm-woocompare-button).

I have tried by using below code but I cant get the anchor.onclick function to work. Any ideas?

Please note that I am looking for a pure javascript solution, no jquery.

<script>
    window.onload = function() {
        var anchors = document.getElementsByClassName('tm-woocompare-button');
        for(var i = 0; i < anchors.length; i++) {
            var anchor = anchors[i];
            anchor.onclick = function() {
                document.getElementsByClassName('front').style.display = 'none';
            }
        }
    }
</script>

Upvotes: 0

Views: 42

Answers (1)

Aditya Shankar
Aditya Shankar

Reputation: 742

try: document.getElementsByClassName('front')[0].style.display = 'none'

instead of: document.getElementsByClassName('front').style.display = 'none';

getElementsByClassName returns an array, rather than an element.

Upvotes: 2

Related Questions