TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Change the All Box Color in HTML
Harshad Pansuriya
Feb 02
2016
Code
1.1
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
$(document).ready(
function
(){
$(
'.box'
).click(
function
() {
$(
this
).addClass(
"blue"
);
var
n = $(
"div.box.blue"
).length;
// check if all boxes are in slateblue
if
(n > 7)
// 1 sec after the last one clicked, make all box red
setTimeout(
function
() {
$(
"div.box.blue"
).css(
"background"
,
"red"
);
}, 1000);
} );
} );
Html code:
<
body
>
<
table
>
<
tr
>
<
td
>
<
div
class
=
"box"
>
</
div
>
</
td
>
<
td
>
<
div
class
=
"box"
>
</
div
>
</
td
>
<
td
>
<
div
class
=
"box"
>
</
div
>
</
td
>
</
tr
>
<
tr
>
<
td
>
<
div
class
=
"box"
>
</
div
>
</
td
>
<
td
>
</
td
>
<
td
>
<
div
class
=
"box"
>
</
div
>
</
td
>
</
tr
>
<
tr
>
<
td
>
<
div
class
=
"box"
>
</
div
>
</
td
>
<
td
>
<
div
class
=
"box"
>
</
div
>
</
td
>
<
td
>
<
div
class
=
"box"
>
</
div
>
</
td
>
</
tr
>
</
table
>
<
div
class
=
"green"
>
</
div
>
</
body
>
Css code:
.box{
height
:
100px
;
width
:
100px
;
border
:
1px
solid
#000
;
margin
:
10px
;
}
.
blue
{
background
:
#6A5ACD
;
}
.
red
{
background
:
#f00
!important
;
}
.
green
{
height
:
24px
;
width
:
24px
;
background
:
#0f0
;
position
:
absolute
;
top:
185px
;
left:
185px
;
}
Jquery code:
$(document).ready(
function
(){
$(
'.box'
).click(
function
() {
$(
this
).addClass(
"blue"
);
var
n = $(
"div.box.blue"
).length;
// check if all boxes are in slateblue
if
(n > 7)
// 1 sec after the last one clicked, make all box red
setTimeout(
function
() {
$(
"div.box.blue"
).css(
"background"
,
"red"
);
}, 1000);
} );
} );
Html
Change the All Box Color using HTML
Css