<!DOCTYPE html>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"UTF-8"
>
<
meta
http-equiv
=
"X-UA-Compatible"
content
=
"IE=edge"
>
<
meta
name
=
"viewport"
content
=
"width=device-width, initial-scale=1.0"
>
<
script
src
=
integrity
=
"sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
crossorigin
=
"anonymous"
>
</
script
>
<
style
>
h1 {
color: #006600;
}
body {
text-align: center;
}
div {
text-align: center;
padding: 5px;
border: 2px solid black;
border-radius: 5px;
margin: 5px;
color: #006600
}
#btn {
margin: 2px;
padding: 5px;
border: 2px solid black;
background-color: #006600;
color: whitesmoke;
width: auto
}
/* The class that turns the div's
background colour to red */
.newClass {
background-color: red;
color: white;
}
</
style
>
</
head
>
<
body
>
<
h1
> GeeksforGeeks</
h1
>
<
p
>
How to set the background color
of the 4th division red by adding
an appropriate class in jQuery?
</
p
>
<
button
id
=
"btn"
>
CLICK TO ADD CLASS TO MAKE 4th DIV RED
</
button
>
<
div
> DIV-1 </
div
>
<
div
> DIV-2 </
div
>
<
div
> DIV-3 </
div
>
<
div
> DIV-4 </
div
>
<
div
> DIV-5 </
div
>
<
script
>
$(document).ready(function () {
// Selecting the body using selector
// finding the 4th div i.e 0 based indexing
// adding a class
$('#btn').click(function () {
$('body div').eq(3).addClass('newClass');
})
});
</
script
>
</
body
>
</
html
>