21 November 2013

More Examples of JQuery Selectors


1.$("*") :  - used to select all elements

Example :
      <!DOCTYPE html>
<html>
  <head>
       <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
       </script>
       <script>
                  $(document).ready(function(){
                            $("button").click(function(){
                                   $("*").hide();
                             });
                  });
       </script>
   </head>
   <body>
           <h2>This is a heading</h2>
           <p>This is a paragraph.</p>
            <p>This is another paragraph.</p>
            <button>Click me</button>
   </body>
</html>

Result :
If Once click on "Click me" button,then all elements are hidden.


 2.$("p.intro") : 

              -used to select all elemnts  with class="intro".

Example :
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p.intro").hide();
  });
});
</script>
</head>
<body>
<h2 class="intro">This is a heading</h2>
<p class="intro">This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>

Result :
If once click on button, elements are hide whichever class="intro" as shown below...

3.$("ul li:first") :

                        used to select the first <li> element of the first <ul>.
Example :
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("ul li:first").hide();
  });
});
</script>
</head>
<body>
<p>List 1:</p>
  <ul>
    <li>Coffee</li>
    <li>Milk</li>
    <li>Tea</li>
  </ul>

<p>List 2:</p>
<ul>
  <li>Coffee</li>
  <li>Milk</li>
  <li>Tea</li>
</ul>
<button>Click me</button>
</body>
</html>

Result :
If click button,whichever <li> elements are first position in <ul> tag are hide as shown below...


4. $("[href]") :

                     used to select all elements with an href attribute.

Example:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("[href]").hide();
  });
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<p><a href="http://www.w3schools.com/html/">HTML Tutorial</a></p>
<p><a href="http://www.w3schools.com/css/">CSS Tutorial</a></p>
<button>Click me</button>
</body>
</html>

Result :
If once click button,then all href related elements are hide as shown below:


5.$("tr:even") :

                      - select all even <tr> elements.
Example :
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("tr:even").css("background-color","yellow");
});
</script>
</head>
<body>
<h1>Welcome to My Web Page</h1>
<table border="1">
<tr>
  <th>Company</th>
  <th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Germany</td>
</tr>
<tr>
<td>Berglunds snabbköp</td>
<td>Sweden</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>UK</td>
</tr>
</table>
</body>
</html>

Result :
even type(0,2,4,.......n) position related <tr> elements shows with yellow backgroungcolor as shown below :

6.$("tr:odd") :

                      - select all odd <tr> elements.

Example :
-------------same code as per above programme--------------

replace the code of even with odd.

odd type(1,3,5,.......n) position related <tr> elements shows with yellow backgroungcolor as shown below :


No comments:

Post a Comment