How to create a bulkhead by class?

  • 0
    <span class="allow"> 1 </span>
    <span class="allow"> 2 </span>
    <span class="allow"> 3 </span>
    <span class="allow"> 4 </span>
    <span class="deny"> 5 </span>

    var d = document.getElementsByClassName("allow");
    d.forEach((e) => { = "backgound: red;"

    How can I change a property of elements through a class? And then I have an error that there is no function even though foreach, as if a function, I do not understand how to solve this.
    Please show on js.
    JavaScript Adam Drake, Jun 22, 2020

  • 2 Answers
  • 0
    You have an HTMLCollection, not an array, so there is no forEach.

    Two options:

    1. [... document.getElementsByClassName ("allow")] to transfer to an array, and then do forEach

    2. If you don't need a Live collection of elements, use querySelectorAll to find the elements.

    Option # 2 is usually preferred.

    And, don't change the entire style property, just the desired style: = 'red';

    Therefore, if you write everything as short as possible, then:

    [...document.querySelectorAll('.allow')].forEach(element => { = 'red';
    Vivienne Coleman

  • 0

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!