jQuery each() loop through elements with the same class

Description

jQuery each() loop through elements with the same class

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Loop Through Elements with the Same Class</title>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<style>
    .box{/*ww w  . j a va  2 s .  co  m*/
        min-height: 20px;
        padding: 15px;
        margin-bottom: 10px;
        border: 1px solid black;
    }
</style>
<script>
$(document).ready(function(){
    // Loop through each div element with the class box
    $(".box").each(function(){
        // Test if the div element is empty
        if($(this).is(":empty")){
              $(this).css("background", "yellow");
        }
    });
});
</script>
</head>
<body>
    <div class="box">A Div box</div>
    <div class="box"></div>
    <div class="box extraclass">Another Div box</div>
    <div class="box">One more Div box</div>
    <div class="box extraclass"></div>
</body>
</html>



PreviousNext

Related