To set styles for all elements with a .box class on jQuery, it’s enough to write the following:
$('.box').css('background', 'red'); How to implement the same on pure JS? Here is my not quite successful attempt:
function $(selector) { var prefix = selector[0]; var selector = selector.substring(1); switch (prefix) { case '#': return document.getElementById(selector); case '.': var elem = document.getElementsByClassName(selector); for (var i = 0; i < elem.length; i++) { return elem[i]; }; default: return document.getElementsByTagName(prefix + selector); } } $('.box').style.background = 'red'; <div> <p class="box">hello</p> <p>world</p> <p class="box">000</p> <p>111</p> </div> The styles applied only to the first element with the .box class, although I return these elements from the loop ...
JS in the process of learning, so please do not strongly "scold" me))
returnyou leave the function, in this case the function$- Grundy