jQuery Examples - Select an element



Problem Description

How to get an element with id = "specialID"?

Solution

Following example would select an element by its id and will apply yellow color to its background.

<html>
   <head>
      <title>The Selector Example</title>
      <script type = "text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>   
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            /* This would select second division only*/
            $("#div2").css("background-color", "yellow");
         });
      </script>  
   </head> 
   
   <body>
      <div class = "big" id = "div1">
         <p>This is first division of the DOM.</p>
      </div>
      <div class = "medium" id = "div2">
         <p>This is second division of the DOM.</p>
      </div>
      <div class = "small" id = "div3">
         <p>This is third division of the DOM</p>
      </div>
   </body> 
</html>

This will produce following result −

jqueryexamples_selectors.htm
Advertisements