This code I written by myself. Here I have 3 main part. When I click on any main part then it sub part will appear. Sub part contain some check boxes. Here I have the following main functionality
- Click the main part, then show it's sub part.If sub part already displayed then hide the sub part
- Highlight selected main part
- At a time only highlight one main part and it's child
- When the the checkbox div inside the sub part is clicked anywhere then the checkbox need to check and highlight that part
Hint:
Main part ( class: filter-3)
Sub Part (class: filter-option-output)
Sub part div (class: common-in-block)
Please see my code
$(function(){
$(".filter-section-color").on("click",function(){
$(".filter-section-pattern").removeClass("output-active");
$(".filter-section-room").removeClass("output-active");
if ( $( this ).hasClass( "output-active" ) ) {
$(".filter-option-output").css("display","none");
$(this).removeClass("output-active");
}
else{
$(".filter-option-output").css("display","none");
$(".color-output").toggle();
$(this).addClass("output-active");
}
});
$(".filter-section-pattern").on("click",function(){
$(".filter-section-color").removeClass("output-active");
$(".filter-section-room").removeClass("output-active");
if ( $( this ).hasClass( "output-active" ) ) {
$(".filter-option-output").css("display","none");
$(this).removeClass("output-active");
}
else{
$(".filter-option-output").css("display","none");
$(".pattern-output").toggle();
$(this).addClass("output-active");
}
});
$(".filter-section-room").on("click",function(){
$(".filter-section-pattern").removeClass("output-active");
$(".filter-section-color").removeClass("output-active");
if ( $( this ).hasClass( "output-active" ) ) {
$(".filter-option-output").css("display","none");
$(this).removeClass("output-active");
}
else{
$(".filter-option-output").css("display","none");
$(".room-output").toggle();
$(this).addClass("output-active");
}
});
$(".color-box").on("click",function(){
$(".color-box").removeClass("active-box");
$(this).addClass("active-box");
var checkbox1 = $(this).find("input[type='checkbox']");
if(checkbox1.is(':checked')){
checkbox1.prop('checked','');
$(this).removeClass("active-box");
}
else{ checkbox1.prop('checked','true'); }
});
$(".pattern-box").on("click",function(){
$(".pattern-box").removeClass("active-box");
$(this).addClass("active-box");
var checkbox2 = $(this).find("input[type='checkbox']");
if(checkbox2.is(':checked')){
checkbox2.prop('checked','');
$(this).removeClass("active-box");
}
else{ checkbox2.prop('checked','true'); }
});
$(".room-box").on("click",function(){
$(".room-box").removeClass("active-box");
$(this).addClass("active-box");
var checkbox3 = $(this).find("input[type='checkbox']");
if(checkbox3.is(':checked')){
checkbox3.prop('checked','');
$(this).removeClass("active-box");
}
else{ checkbox3.prop('checked','true'); }
});
});
body{
background-color: #d6e9d880 !important;
}
.filter-section-p{
margin-bottom: 8px;
}
.filter-arrow{
margin-top: 5px;
float: right;
}
.filter-section{
background: #E5E5E599;
text-align:center;
padding-top: 18px !important;
padding-bottom: 41px;
}
.filter-section-h2{
margin-bottom: 2px !important;
font-size: 24px;
text-transform: uppercase;
}
.filter-icon{
width:35px;
float:left;
}
.filter-inside-p{
float:left;
text-transform: uppercase;
font-size: 16px;
margin-left: 15px;
margin-bottom: 0px;
}
.filter-3{
border:1px solid #912C5E33;
float:left;
width:27%;
margin-left: 3%;
padding: 6px;
cursor:pointer;
}
.filter-main-div{
margin-left: 49px;
margin-bottom:3px;
}
.filter-option-output{
border-left: 1px solid #93A8B733;
border-right: 1px solid #93A8B733;
background: white;
position: absolute;
margin-top: 58px;
z-index: 3;
display:none;
}
.color-backround{
float:left;
min-width: 47px;
height: 32px;
}
.color-name{
margin-left: 12px !important;
font-size: 14px !important;
text-transform: uppercase !important;
margin-top: 4px !important;
float:left !important;
margin-bottom: 15px;
}
.color-value{
float: right !important;
margin-top: 11px !important;
margin-right: 13px !important;
}
.common-in-block{
float:left;
width:30%;
margin-left: 3%;
padding-top: 11px;
border-right: 1px solid #80808033;
padding-bottom: 0px !important;
cursor:pointer;
}
.output-active{ background:white !important; border:2px solid #912C5E33 !important; }
.color-block { border-bottom: 1px solid #80808033; overflow: hidden; padding-bottom: 4px; margin-top:4px; }
.active-box { background:#f5deb380; }
.color-black{ background-color:#252525; }
.color-blue{ background-color:#99CEE8; }
.color-brown{ background-color:#96776B; }
.color-cream{ background-color:#EAE3D9; }
.color-green{ background-color:#BED6A4; }
.color-grey{ background-color:#919191; }
.color-orange{ background-color:#FF9D78; }
.color-pink{ background-color:#F9C5CA; }
.color-purple{ background-color:#AF9EC7; }
.color-red{ background-color:#E95A63; }
.color-silver{ background-color:#DDDDDD; }
.color-white{ background-color:#F8F8F8; }
<html>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<body>
<div class=" col-sm-12 filter-main-div">
<div class="col-sm-8 filter-main">
<div class="filter-3 filter-section-color">
<img src="https://image.flaticon.com/icons/svg/401/401122.svg" class="filter-icon" />
<p class="filter-inside-p"> Part1</p>
<i class="icon-chevron-down filter-arrow"></i>
</div>
<div class="filter-3 filter-section-pattern">
<img src="https://image.flaticon.com/icons/svg/401/401122.svg" class="filter-icon" />
<p class="filter-inside-p">Part2</p>
<i class="icon-chevron-down filter-arrow"></i>
</div>
<div class="filter-3 filter-section-room">
<img src="https://image.flaticon.com/icons/svg/401/401122.svg" class="filter-icon"/>
<p class="filter-inside-p">Part3</p>
<i class="icon-chevron-down filter-arrow"></i>
</div>
</div>
<div class="col-sm-8 filter-option-output color-output">
<div class="color-block">
<div class="common-in-block color-box">
<div class="color-backround color-cream"> </div><p class="color-name">Part1-child1</p>
<input type="checkbox" value="cream" class="color-value">
</div>
<div class="common-in-block color-box">
<div class="color-backround color-green"> </div><p class="color-name">Part1-child2</p>
<input type="checkbox" value="green" class="color-value">
</div>
<div class="common-in-block color-box">
<div class="color-backround color-grey"> </div><p class="color-name">Part1-child3</p>
<input type="checkbox" value="grey" class="color-value">
</div>
</div>
</div>
<div class="col-sm-8 filter-option-output pattern-output">
<div class="color-block">
<div class="common-in-block pattern-box">
<div class="color-backround color-black"> </div><p class="color-name">Part-2-child1</p>
<input type="checkbox" value="black" class="color-value">
</div>
<div class="common-in-block pattern-box">
<div class="color-backround color-blue"> </div><p class="color-name">Part-2-child2</p>
<input type="checkbox" value="blue" class="color-value">
</div>
<div class="common-in-block pattern-box">
<div class="color-backround color-red"> </div><p class="color-name">Part-2-child3</p>
<input type="checkbox" value="brown" class="color-value">
</div>
</div>
</div>
<div class="col-sm-8 filter-option-output room-output">
<div class="color-block">
<div class="common-in-block room-box">
<div class="color-backround color-black"> </div><p class="color-name">Part#3-child1</p>
<input type="checkbox" value="black" class="color-value">
</div>
<div class="common-in-block room-box">
<div class="color-backround color-blue"> </div><p class="color-name">Part#3-child2</p>
<input type="checkbox" value="blue" class="color-value">
</div>
<div class="common-in-block room-box">
<div class="color-backround color-red"> </div><p class="color-name">Part#3-child3</p>
<input type="checkbox" value="brown" class="color-value">
</div>
</div>
</div>
</div>
</body>
</html>
Please copy this code into your localhost and check it.
Here all of my functionality is working perfectly. But what I feel that the logic I applied is not that much good and still I can reduce the code. Please check this code and suggest which part I can still reduce the code.