2 Example(s) of JQuery Selectors


Description :

jQuery selectors allow you to select and manipulate HTML element(s).


JQuery Selectors Example - 1
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>

<script>
$(document).ready(function(){
  $("p:visible").css("background-color", "lightblue");
});
</script>
</head>

<body>

<h1>This is a Dummy Text</h1>

<p style="display: none">(1) APPWRK IT Solutions is a complete Web Solutions Company.</p>
<p>(2) APPWRK IT Solutions is a complete Web Solutions Company.</p>
<p style="display:none">(3) APPWRK IT Solutions is a complete Web Solutions Company.</p>
<p>(4) APPWRK IT Solutions is a complete Web Solutions Company.</p>
<p>(5) APPWRK IT Solutions is a complete Web Solutions Company.</p>

</body>
</html>

Output

Description :

In this example The children() method returns all direct children of the selected element.


JQuery Selectors Example - 2
<!DOCTYPE html>
<html>
<head>
<style>
.descendants * { 
  display: block;
  border: 2px solid lightgrey;
  color: lightgrey;
  padding: 5px;
  margin: 15px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("ul").children().css({"color": "cyan", "border": "2px solid green"});
});
</script>
</head>

<body class="descendants">body will be great-grandparent
  <div style="width:700px;">div will grandparent
    <ul>ul will be direct parent  
      <li>li will be child
        <span>span will be grandchild</span>
      </li>
    </ul>   
  </div>
</body>
</html>

Output