Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

jQuery: Finds even table rows, matching the first, third and so on

jQuery Fundamental - II : Exercise-88

Finds even table rows, matching the first, third and so on (index 0, 2, 4 etc.)

<body>
<table border="1"> 
<tr><td>R1C1  Index-0</td><td>R1C2 Index-0</td></tr> 
<tr><td>R2C1  Index-0</td><td>R2C2 Index-1</td></tr> 
<tr><td>R3C1  Index-0</td><td>R3C2 Index-2</td></tr> 
<tr><td>R4C1  Index-0</td><td>R4C2 Index-3</td></tr> 
<tr><td>R5C1  Index-0</td><td>R5C2 Index-4</td></tr> 
lt;/table> 
<button  id="button1">Click to see the effect</button>  
</body> 

Sample solution :

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-git.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Finds even table rows, matching the first, third  and so on.</title>
<style type="text/css">
button {
display: block;
margin: 20px 0 0 0;
}
</style>
</head>
<body>
<table border="1">
<tr><td>R1C1 Index-0</td><td>R1C2 Index-0</td></tr>
<tr><td>R2C1 Index-0</td><td>R2C2 Index-1</td></tr>
<tr><td>R3C1 Index-0</td><td>R3C2 Index-2</td></tr>
<tr><td>R4C1 Index-0</td><td>R4C2 Index-3</td></tr>
<tr><td>R5C1 Index-0</td><td>R5C2 Index-4</td></tr>
</table>
<button id="button1">Click to see the effect</button>
</body>
</html>

JavaScript Code :

$('#button1').click(function(){
$( "tr:even" ).css( "background-color", "orange" );
});

See the Pen jquery-fundamental-exercise-88 by w3resource (@w3resource) on CodePen.


Contribute your code and comments through Disqus.

Previous: Find the sixth cell (second row and third column ) of a 3x3 table.
Next: Check whether the META key was pressed when the event fired.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.