Hide boxes by clicking on it using jQuery


 

Hide boxes by clicking on it using jQuery

In this tutorial , we will discuss about how to hide boxes by clicking on it using jQuery.

In this tutorial , we will discuss about how to hide boxes by clicking on it using jQuery.

Hide boxes by clicking on it using jQuery

In this tutorial , we will discuss about how to hide boxes by clicking on it using jQuery. In the given below example , 30 boxes inside web page are displayed. When we click on any box (among these 30 boxes), it  disappears and the next box (if any) shifts to left. These hide action is due to event click.

jqHideboxonclick.html     

<!DOCTYPE html>
<html>
<head>
<style>
div { background:#D2691E; width:30px;
height:40px; margin:2px; float:left; }
</style>
<script src="jquery-1.4.2.js" type="text/javascript"></script>
</head>
<body>
<div></div>
<script>
for (var i = 0; i < 30; i++) {
$("<div>").appendTo(document.body);
}
$("div").click(function () {
$(this).hide(2000, function () {
$(this).remove();
});
});
</script>
</body>
</html>

Description of the program :

Inside "div" ,box is created using 'html' and it is replicated by using "for " loop inside 'script' as given below :

for (var i = 0; i < 30; i++) {
$("<div>").appendTo(document.body);
}

When we click on any box ,it would disappear due to following code :

$("div").click(function () {
$(this).hide(2000, function () {
$(this).remove();
});
});

OUTPUT

After clicking 15 boxes :

Download Source Code

Click here to see demo

Ads