How to interact with all classes?

  • 0
    You need to get all the buttons from the block. By clicking on one of them, the button should receive the 'focused' class, if another button is selected, then the class should switch to it. The code only works with getElementsById

    <button value="Одностраничный сайт" id="brief-input"  class="contacts__info brief-input">Одностраничный сайт</button>
             <button value="Корпоративный сайт" class="contacts__info brief-input">Корпоративный сайт</button>
             <button value="Интернет магазин"class="contacts__info brief-input">Интернет магазин</button>
             <button value="Я не знаю" class="contacts__info brief-input">Я не знаю</button>

    Js code
    var brief = document.getElementsByClassName('brief-input')
    brief.onclick = function () {
    JavaScript Eloise Crane, Feb 13, 2020

  • 3 Answers
  • 0
    The most convenient way is to use querySelectorAll () , which will create a collection of elements. Which has to be iterated over and hung by the click listeners. Or just iterate over the collection that you collect now.

  • 0

  • 0
    Not getElementsById, but singular getElementById! And getElementsByClassName returns not one element, but an array-like object. As far as I can tell, array-like is not enough for it to have a native forEach method, but you can call forEach from a real Array:
    function (element) { console.log(element.href); });

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