Kosmos Web Design Kosmos Web Design - 2 months ago 7
jQuery Question

How do I select a div based on $(this) selector in jQuery?

My code looks something like this:



$(document).ready(function(){
"use strict";
$(".box_button").click(function(){
var content_front = $(this).closest('.front');
var content_back = $(this).parents('.box_content');

content_front.addClass("flip_1");
content_back.addClass("flip_2");
});
});

body {
background: #f4f4f4;
}

.col_x4 {
position: relative;
display: block;
float: left;
width: 300px;
height: auto;
box-sizing: border-box;
padding: 7px;
}

.box {
position: relative;
display: block;
box-sizing: border-box;
width: 100%;
border-radius: 4px;
perspective: 1000px;
}

.box_top {
position: relative;
display: block;
box-sizing: border-box;
width: 100%;
overflow: hidden;
border-radius: 4px 4px 0 0;
border: 1px solid #e3e3e3;
border-bottom: hidden;
}

.box_top img {
position: relative;
display: block;
max-width: 100%;
height: auto;
-webkit-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
}

.box_top img:hover {
-webkit-transform: scale(1.2,1.2);
transform: scale(1.2,1.2);
}

.box_content {
position: relative;
display: block;
box-sizing: border-box;
transform-style: preserve-3d;
}

.front {
position: absolute;
display: block;
box-sizing: border-box;
top: 0; left: 0;
width: 100%;
height: auto;
background-color: #fff;
z-index: 2;
transform: rotateY(0deg);
transition: all 0.6s ease-in-out;
border: 1px solid #e3e3e3;
min-height: 281px;
}

.back {
position: absolute;
display: block;
box-sizing: border-box;
top: 0; left: 0;
width: 100%;
height: auto;
z-index: 1;
background-color: #fff;
transform: rotateY(180deg);
transition: all 0.6s ease-in-out;
backface-visibility: hidden;
border: 1px solid #e3e3e3;
min-height: 281px;
}

.flip_1 {
z-index: 1;
transform: rotateY(180deg);
backface-visibility: hidden;
}

.flip_2 {
z-index: 2;
transform: rotateY(360deg);
backface-visibility: visible;
}

.box_title {
position: relative;
margin: 10px 0;
padding: 0;
text-align: center;
}

.t1::after,
.t2::after {
content: "";
position: relative;
display: block;
width: 100%;
height: 20px;
margin: 10px 0 0;
background: url(https://pixabay.com/static/uploads/photo/2012/05/07/02/47/green-47700_960_720.png) no-repeat center center;
background-size: contain;
}

.t2::before {
content: "";
position: relative;
display: block;
width: 100%;
height: 90px;
background: url(../images/basket-full.png) no-repeat center center;
background-size: contain;
margin: 25px 0 5px;
}

.box_description {
position: relative;
display: block;
box-sizing: border-box;
width: 90%;
margin: 0 5% 15px;
padding: 0 15px;
text-align: center;
line-height: 20px;
max-height: 100px;
overflow: auto;
}

.box_description::-webkit-scrollbar
{
width: 4px; /* for vertical scrollbars */
height: 4px; /* for horizontal scrollbars */
}

.box_description::-webkit-scrollbar-track
{
background: #e3e3e3;
border-radius: 2px;
}

.box_description::-webkit-scrollbar-thumb
{
background: rgba(61,82,95,0.3);
border-radius: 2px;
}

.box_price {
position: relative;
display: block;
width: 90%;
margin: 0 5% 15px;
text-align: center;
font-size: 18px;
font-weight: 600;
}

.box_button {
z-index: 3;
position: relative;
display: block;
box-sizing: border-box;
width: 70%;
height: 36px;
margin: 0 auto 15px;
line-height: 36px;
color: #333;
background-color: #f4f4f4;
border-width: 1px;
border-style: solid;
border-radius: 18px;
text-align: center;
cursor: pointer;
}

.btn_1 {
border-color: #008CFF;
}

.btn_2 {
border-color: #F89A00;
}

.btn_active {
z-index: 4;
-webkit-animation: scale_up 0.6s linear;
animation: scale_up 0.6s linear;
}

@-webkit-keyframes scale_up {
0% {
-webkit-transform: scale(1,1);
transform: scale(1,1);
}
50% {
-webkit-box-shadow: 0px -8px 55px -15px rgba(0,0,0,0.75);
-moz-box-shadow: 0px -8px 55px -15px rgba(0,0,0,0.75);
box-shadow: 0px -8px 55px -15px rgba(0,0,0,0.75);
-webkit-transform: scale(1.1,1.1);
transform: scale(1.1,1.1);
}
100% {
-webkit-transform: scale(1,1);
transform: scale(1,1);
}
}

@keyframes scale_up {
0% {
-webkit-transform: scale(1,1);
transform: scale(1,1);
}
50% {
-webkit-box-shadow: 0px -8px 55px -15px rgba(0,0,0,0.75);
-moz-box-shadow: 0px -8px 55px -15px rgba(0,0,0,0.75);
box-shadow: 0px -8px 55px -15px rgba(0,0,0,0.75);
-webkit-transform: scale(1.1,1.1);
transform: scale(1.1,1.1);
}
100% {
-webkit-transform: scale(1,1);
transform: scale(1,1);
}
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col_x4">
<div class="box">
<div class="box_top">
<img src="http://oregonaitc.org/wp-content/uploads/2016/02/potato.jpg" alt="product" />
</div>
<div class="box_content">
<div class="front">
<h2 class="box_title t1">Title</h2>
<p class="box_description">Description</p>
<div class="box_price">$10</div>
<a class="box_button btn_1">Add to cart</a>
</div>
<div class="back">
<h2 class="box_title t2">Flip side title</h2>
<p class="box_description">Product added to basket.</p>
<a class="box_button btn_1">Pay</a>
</div>
</div>
</div>
</div>





How do I select div "back" based on the click of the button using $(this) selector assigned to the box? I don't want to use unique ID's or classes to specify exactly which button in which box is being clicked.

I used the .closest method to select div "front". How do I do the same for div "back"?

Answer

Find the main container first and work from there:

$(".box_button").click(function(){
    var $box = $(this).parents('.box');
    var content_front = $box.find('.front');
    var content_back = $box.find('.back');

    content_front.addClass("flip_1");   
    content_back.addClass("flip_2");
});

$(document).ready(function(){
	"use strict";
	$(".box_button").click(function(){
        var $box = $(this).parents('.box');
		var content_front = $box.find('.front');
		var content_back = $box.find('.back');
		
		content_front.addClass("flip_1");	
		content_back.addClass("flip_2");
	});
});
body {
  background: #f4f4f4;  
}

.col_x4 {
	position: relative;
	display: block;
	float: left;
	width: 300px;
	height: auto;
	box-sizing: border-box;
	padding: 7px;	
}

.box {
	position: relative;
	display: block;
	box-sizing: border-box;
	width: 100%;
	border-radius: 4px;
	perspective: 1000px;
}

.box_top {
	position: relative;
	display: block;
	box-sizing: border-box;
	width: 100%;
	overflow: hidden;
	border-radius: 4px 4px 0 0;
	border: 1px solid #e3e3e3;
	border-bottom: hidden;
}

.box_top img {
	position: relative;
	display: block;
	max-width: 100%;
	height: auto;
	-webkit-transition: all 0.3s ease-in-out;
	transition: all 0.3s ease-in-out;
}

.box_top img:hover {
	-webkit-transform: scale(1.2,1.2);
	transform: scale(1.2,1.2);
}

.box_content {
	position: relative;
	display: block;
	box-sizing: border-box;
	transform-style: preserve-3d;
}

.front {
	position: absolute;
	display: block;
	box-sizing: border-box;
	top: 0; left: 0;
	width: 100%;
	height: auto;
	background-color: #fff;
	z-index: 2;
	transform: rotateY(0deg);
	transition: all 0.6s ease-in-out;
	border: 1px solid #e3e3e3;
	min-height: 281px;
}

.back {
	position: absolute;
	display: block;
	box-sizing: border-box;
	top: 0; left: 0;
	width: 100%;
	height: auto;
	z-index: 1;
	background-color: #fff;
	transform: rotateY(180deg);
	transition: all 0.6s ease-in-out;
	backface-visibility: hidden;
	border: 1px solid #e3e3e3;
	min-height: 281px;
}

.flip_1 {
	z-index: 1;
	transform: rotateY(180deg);
	backface-visibility: hidden;
}

.flip_2 {
	z-index: 2;
	transform: rotateY(360deg);
	backface-visibility: visible;
}

.box_title {
	position: relative;
	margin: 10px 0;
	padding: 0;
	text-align: center;	
}

.t1::after,
.t2::after {
	content: "";
	position: relative;
	display: block;
	width: 100%;
	height: 20px;
	margin: 10px 0 0;
	background: url(https://pixabay.com/static/uploads/photo/2012/05/07/02/47/green-47700_960_720.png) no-repeat center center;
  background-size: contain;
}

.t2::before {
	content: "";
	position: relative;
	display: block;
	width: 100%;
	height: 90px;
	background: url(../images/basket-full.png) no-repeat center center;
	background-size: contain;
	margin: 25px 0 5px;
}

.box_description {
	position: relative;
	display: block;
	box-sizing: border-box;
	width: 90%;
	margin: 0 5% 15px;
	padding: 0 15px;
	text-align: center;
	line-height: 20px;
	max-height: 100px;
	overflow: auto;
}

.box_description::-webkit-scrollbar
{
  width: 4px;  /* for vertical scrollbars */
  height: 4px; /* for horizontal scrollbars */
}

.box_description::-webkit-scrollbar-track
{
  background: #e3e3e3;
  border-radius: 2px;
}

.box_description::-webkit-scrollbar-thumb
{
  background: rgba(61,82,95,0.3);
  border-radius: 2px;
}

.box_price {
	position: relative;
	display: block;
	width: 90%;
	margin: 0 5% 15px;
	text-align: center;
	font-size: 18px;
	font-weight: 600;	
}

.box_button {
	z-index: 3;
	position: relative;
	display: block;
	box-sizing: border-box;
	width: 70%;
	height: 36px;
	margin: 0 auto 15px;
	line-height: 36px;
	color: #333;
	background-color: #f4f4f4;
	border-width: 1px;
	border-style: solid;
	border-radius: 18px;
	text-align: center;
	cursor: pointer;
}

.btn_1 {
	border-color: #008CFF;
}

.btn_2 {
	border-color: #F89A00;
}

.btn_active {
	z-index: 4;
	-webkit-animation: scale_up 0.6s linear;
	animation: scale_up 0.6s linear;	
}

@-webkit-keyframes scale_up {
  0% {
	  	-webkit-transform: scale(1,1);
		transform: scale(1,1);
  }
  50% {
		-webkit-box-shadow: 0px -8px 55px -15px rgba(0,0,0,0.75);
		-moz-box-shadow: 0px -8px 55px -15px rgba(0,0,0,0.75);
		box-shadow: 0px -8px 55px -15px rgba(0,0,0,0.75);
		-webkit-transform: scale(1.1,1.1);
		transform: scale(1.1,1.1);
  }
  100% {
		-webkit-transform: scale(1,1);
		transform: scale(1,1);
  }
}

@keyframes scale_up {
  0% {
	  	-webkit-transform: scale(1,1);
		transform: scale(1,1);
  }
  50% {
		-webkit-box-shadow: 0px -8px 55px -15px rgba(0,0,0,0.75);
		-moz-box-shadow: 0px -8px 55px -15px rgba(0,0,0,0.75);
		box-shadow: 0px -8px 55px -15px rgba(0,0,0,0.75);
		-webkit-transform: scale(1.1,1.1);
		transform: scale(1.1,1.1);
  }
  100% {
		-webkit-transform: scale(1,1);
		transform: scale(1,1);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col_x4">
	<div class="box">
    	<div class="box_top">
        	<img src="http://oregonaitc.org/wp-content/uploads/2016/02/potato.jpg" alt="product" />
        </div>
        <div class="box_content">
        	<div class="front">
            	<h2 class="box_title t1">Title</h2>
                <p class="box_description">Description</p>
                <div class="box_price">$10</div>
                <a class="box_button btn_1">Add to cart</a>
            </div>
            <div class="back">
            	<h2 class="box_title t2">Flip side title</h2>
                <p class="box_description">Product added to basket.</p>
                <a class="box_button btn_1">Pay</a>
            </div>
        </div>
    </div>
</div>