w3resource

jQuery: Set the background color of the 4th division red by adding an appropriate class

jQuery Fundamental - II : Exercise-86

Set the background color of the 4th division red by adding an appropriate class.

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>Set the background color of the 4th  division red by adding an appropriate class.</title>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<button id="button1">Click to see the effect</button>
</body>
</html>

CSS Code :

 div {
  width: 20px;
  height: 20px;
  margin: 5px;
  float: left;
  border: 2px solid green;
  }
  .red {
  background: red;
  }
 button {
  display: block;
  margin: 20px 0 0 0;
  }

JavaScript Code :

$('#button1').click(function(){ 
$( "body" ).find( "div" ).eq( 3 ).addClass( "red" );
});

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


Contribute your code and comments through Disqus.

Previous: Selects all paragraphs and finds span elements inside these and reverts the selection back to the paragraphs.
Next: Find the sixth cell (second row and third column ) of a 3x3 table.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/jquery-exercises/2/jquery-fundamental-exercise-86.php