How jQuery selects elements using CSS?

jQueryWeb DevelopmentFront End Technology

jQuery uses CSS selector to select elements using CSS. Let us see an example to return a style property on the first matched element. The css( name ) method returns a style property on the first matched element.

Here is the description of all parameter used by this method −

  • name − The name of the property to access.

Example

You can try to run the following code to learn how jQuery selects elements with CSS:

Live Demo

<html>

   <head>
      <title>jQuery Example</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
       
      <script>
         $(document).ready(function() {
           
            $("div").click(function () {
               var color = $(this).css("background-color");
               $("#result").html("That div is <span style = 'color:" +
                  color + ";'>" + color + "</span>.");
            });
               
         });
      </script>
       
      <style>
         div { width:60px; height:60px; margin:5px; float:left; }
      </style>
   </head>
   
   <body>
   
      <p>Click on any square:</p>
      <span id = "result"> </span>
       
      <div style = "background-color:blue;"></div>
      <div style = "background-color:gray;"></div>
      <div style = "background-color:#123456;"></div>
      <div style = "background-color:#f11;"></div>
       
   </body>
   
</html>
raja
Published on 17-Dec-2017 08:48:44
Advertisements