JQuery Selector id

From CoderLearner
Jump to: navigation, search

Description

Select a single element based on a given id attribute.

Syntax

$('#id');

Example 1

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Select</title>
      <script 
         type="text/javascript" 
         src="/lib/jquery-1.7.2.min.js">				 
      </script>
      <style>
         .mark1 {
            border:1px solid #999900;
            width:130px;
         }
         .mark2 {
            background-color:green;
            width:230px;
         }

      </style>
      <script>
         $().ready(function() {
            $('div#top1').addClass('mark1');
            $('div#top2').addClass('mark2');
         });
      </script>
   </head>
   <body> 
      <div id="top1">
         <ul>
            <li>One</li>
            <li>Two</li>
            <li>Three</li>
         </ul>
         <div  id="top2">
            <ul>
               <li>1</li>
               <li>2</li>
            </ul>
         </div>
      </div>
   </body>
</html>

Try It