0

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>
1
  • Do you have a working example of this? Commented Sep 29, 2018 at 19:39

1 Answer 1

2

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

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

getElementsByClassName returns an array, rather than an element.

Sign up to request clarification or add additional context in comments.

4 Comments

Worked perfect!:)
Uhm, actually it did not. I forgot to say in my question that there is a lot of span classes with the link, and all has to be removed when you hit the button. [0] makes only the first link to go away, then the function stops and the others is still showing. Any idea how to make the function run through all of them?
try running a for loop through all the elements you can do some thing like, var elements = document.getElementsByClassName('front')
try doing it yourself, ask if you're stuck for the exact code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.