Select every element in an element list - Javascript jQuery

Javascript examples for jQuery:Selector

Description

Select every element in an element list

Demo Code

ResultView the demo in separate window

<html>
 <head> 
  <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.1.min.js"></script> 
 </head> /*from  w  w  w  .j av  a2 s .c om*/
 <body> 
  <p class="itemclass">Element 1.</p> 
  <p class="itemclass">Element 2.</p> 
  <p class="itemclass">Element 3.</p> 
  <button onclick="myFunction()">Try it</button> 
  <script>
function myFunction() {
    var items = document.getElementsByClassName("itemclass");
    $.each(items, function(index, item){
        item.style.backgroundColor = "red";
    });
}

      </script>  
 </body>
</html>

Related Tutorials