Traverse elements with same class

Asked

Viewed 19 times

0

I saw similar content on the site, but as I am starting I still did not understand well in solving the following problem:

I have some inputs that call a function that tests whether the value of the clicked input is equal to a pre-defined string. The problem is that there are 4 inputs that have the same class and when I run querySelector() it brings the first element. What I need is for the function to go through all the elements of the sought class and check if the value of each one is the one I’m looking for. Below I provide a hypothetical code simulating the situation. In this example, the correct answer would be "C". However, the querySelector does not reach there. I need some help in pure javascript

Which is the correct option?

<script>
    function testarResposta() {
        var resposta = document.querySelectorAll('.opcao')
        if (resposta == 'C') {
            document.querySelector('.container').innerHTML = "<p>Voce acertou!</p>"
        } else {
            document.querySelector('.container').innerHTML = "<p>Voce errou!</p>"
        }
    }
</script>
  • Basically, just do what the linked questions above suggest: take the result of querySelectorAll and use a for to traverse the elements

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.