I'm trying to create boxes that show text when expanded and hide again when contracted, but I can't figure out how to trigger the events on child elements only when a box is clicked, instead all children of all boxes are affected when any box is clicked.
Here's my code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.div{
height: 50px;
width: 100px;
background-color: grey;
}
.cont{
}
.expa{
display:none;
}
</style>
<script src="jquery-1.11.1.js"></script>
</head>
<body>
<div class=div>
<p class=cont>Caption</p>
<p class=expa>String to show when expanded and hide when contracted</p>
</div>
<br>
<div class=div>
<p class=cont>Caption</p>
<p class=expa>String to show when expanded and hide when contracted</p>
</div>
<script>
$(document).ready(function(){
var contracted = true;
$(".div").click(function(){
if(contracted){
$(this).animate({height:"200px", width:"600px"}, 200);
$(".cont").css({display: 'none'});
$(".expa").delay(200).fadeIn(200);
contracted = false;
}else{
$(this).animate({height:"50px", width:"100px"}, 200);
$(".cont").delay(200).fadeIn(200);
$(".expa").css({display: 'none'});
contracted = true;
};
});
});
</script>
</body>
</html>
The problem is that clicking any .div element triggers ALL elements of the .cont and .expa classes and not only those specific for the clicked .div.
Search on Stackoverflow and elsewhere only deals with issues with triggering a parent element event by interacting with a child element.
I've fiddled around with .on(), .parent(), .children() and gone through the jQuery documentation without finding anything.