How to Show/Hide divs with the same class name individually? - javascript

I've tried to implement answers I found on stackoverflow asked by other people, but I'm not getting the results that I want.
I have an about section on a site I'm building. In that section I have four different about items. At the bottom is a button and when that button is clicked I want the four boxes to be hidden and for a large about item to appear(show) in their place and display the information of the about item who's button was clicked. With the code I have now, when I click one of the buttons, all large about item divs show up instead of just one. Is it possible to just display only one large about item(showfullabout) when clicked, instead of all four? I want to individually show items when clicked.
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 showfullabout">
<div class="about-item-full">
<h1>Title</h1>
<h2>Description</h2>
<p>Content</p>
<button class="small-about"><i class="fa fa-plus"></i>
</button>
</div>
</div>
<div class="col-xs-12 col-sm-6- col-md-3 col-lg-3">
<div class="about-item">
<div class="about-item-top">
<img src="img/img07.jpg" alt="img">
<div class="about-item-circle">
<span class="oi oi-justify-left"></span>
</div>
</div>
<div class="about-item-bottom">
<h1>Title</h1>
<h2>Description</h2>
<p>
Content
</p>
<button class="full-about"><i class="fa fa-plus"></i>
</button>
</div>
</div>
<!--END ITEM-->
</div>
<!--END COLUMN-->
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 showfullabout">
<div class="about-item-full">
<h1>Title</h1>
<h2>Description</h2>
<p>Content</p>
<button class="small-about"><i class="fa fa-plus"></i>
</button>
</div>
</div>
<div class="col-xs-12 col-sm-6- col-md-3 col-lg-3">
<div class="about-item">
<div class="about-item-top">
<img src="img/img07.jpg" alt="img">
<div class="about-item-circle">
<span class="oi oi-image"></span>
</div>
</div>
<div class="about-item-bottom">
<h1>Title</h1>
<h2>Description</h2>
<p>
Content
</p>
<button class="full-about"><i class="fa fa-plus"></i>
</button>
</div>
</div>
<!--END ITEM-->
</div>
<!--END COLUMN-->
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 showfullabout">
<div class="about-item-full">
<h1>Title</h1>
<h2>Description</h2>
<p>Content</p>
<button class="small-about"><i class="fa fa-plus"></i>
</button>
</div>
</div>
<div class="col-xs-12 col-sm-6- col-md-3 col-lg-3">
<div class="about-item">
<div class="about-item-top">
<img src="img/img07.jpg" alt="img">
<div class="about-item-circle">
<span class="oi oi-monitor"></span>
</div>
</div>
<div class="about-item-bottom">
<h1>Title</h1>
<h2>Description</h2>
<p>
Content
</p>
<button class="full-about"><i class="fa fa-plus"></i>
</button>
</div>
</div>
<!--END ITEM-->
</div>
<!--END COLUMN-->
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 showfullabout">
<div class="about-item-full">
<h1>Title</h1>
<h2>Description</h2>
<p>Content</p>
<button class="small-about"><i class="fa fa-plus"></i>
</button>
</div>
</div>
<div class="col-xs-12 col-sm-6- col-md-3 col-lg-3">
<div class="about-item">
<div class="about-item-top">
<img src="img/img07.jpg" alt="img">
<div class="about-item-circle">
<span class="oi oi-location"></span>
</div>
</div>
<div class="about-item-bottom">
<h1>Title</h1>
<h2>Description</h2>
<p>
Content
</p>
<button class="full-about"><i class="fa fa-plus"></i>
</button>
</div>
</div>
<!--END ITEM-->
</div>
<!--END COLUMN-->
<!--CSS-->
.showfullabout{
display:none;
}
<!--Script-->
$("button.full-about").click(function(){
$(".about-item").hide();
$(".showfullabout").fadeToggle(500);
});
$("button.small-about").click(function(){
$(".about-item").fadeToggle(500);
$(".showfullabout").hide();
});

You need to use relative selectors to find the target elements
$("button.full-about").click(function () {
$(".about-item").hide();
$(this).closest(".about-item").parent().prev('.showfullabout').fadeIn(500);
});
$("button.small-about").click(function () {
$(this).closest(".showfullabout").hide();
$(".about-item").fadeToggle(500);
});
Demo: Fiddle
In the case of button full-about you need to hide all about-item elements and show the previous sibling of the ancestor about-item's parent, the opposite for small-about button

Related

Bootstrap5 tab content on second tab creates unlimited white space

in my project I want to show statistics for the current month and the current year. To split this statistics I created a card with tabs that split month and year.
The month tab is loaded as default. If i now go on the year tab everything is loading but then it will load unlimited empty "stuff / white data" (I don't know how to explain it better), and you can't use this tab because the site will be bigger and bigger. There is also no error in the
This has first been poped up after I changed alle my charts (chart.js) to an dynamic way that you can go e.g. to the next month or the month before. If I load all charts with "fixed" data then the tab works correctly.
What is the problem?
Actual look:
How it has to be looked:
After some testing i see this:
Code snippet:
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta2/css/all.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/#popperjs/core#2.9.2/dist/umd/popper.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="card-header">
<div>
<ul class="nav nav-pills nav-fill mt-4" role="tablist">
<li class="nav-item active">
<a class="nav-link" data-bs-toggle="tab" href="#Month" role="tab">
<span><strong>Current Month</strong></span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" data-bs-toggle="tab" href="#Year" role="tab">
<span><strong>Current Year</strong></span>
</a>
</li>
</ul>
</div>
</div>
<div class="card-body">
<div class="tab-content">
<div class="tab-pane active" id="Month" role="tabpanel">
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="card-header d-flex justify-content-between align-items-center">
<h2 class="card-title" id="month"></h2>
<div class="justify-content-end">
<button class="btn btn-info last-month" id="last-month"><i class="fas fa-chevron-left"></i></button>
<button class="btn btn-info current-month" id="current-month">Current Month</button>
<button class="btn btn-info next-month" id="next-month"><i class="fas fa-chevron-right"></i></button>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-header bg-inverse">
<h2 class="card-title text-white" id="monthly_training_sessions"></h2>
</div>
<div class="card-body">
<span class="badge chart-label-intervall">Intervall</span>
<span class="badge chart-label-longrun">Longrun</span>
<span class="badge chart-label-speedwork">Speedwork</span>
<span class="badge chart-label-stabilisation">Stabilisation</span>
<span class="badge chart-label-competition">Competition</span>
<span class="badge chart-label-cycling">Cycling</span>
<span class="badge chart-label-swimming">Swimming</span>
<br><br>
<canvas id="monthy_session_stats" width="180" height="180"></canvas>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-header bg-inverse d-flex justify-content-between">
<h2 class="card-title text-white" id="monthly_training_days"></h2>
<h2 class="card-title text-white" id="monthly_injury_days"></h2>
</div>
<div class="card-body">
<div class="d-flex justify-content-evenly" style="margin-bottom: 18px">
<span class="badge chart-label-training">Training</span>
<span class="badge chart-label-injury">Free</span>
<span class="badge chart-label-free">Injury</span>
</div>
<br>
<canvas id="monthy_training_stats" width="180" height="180"></canvas>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-header bg-inverse">
<h2 class="card-title text-white">Shoes</h2>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table">
<tbody id="monthly_shoes_stats">
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="tab-pane" id="Year" role="tabpanel">
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="card-header d-flex justify-content-between align-items-center">
<h2 class="card-title" id="year"></h2>
<div class="justify-content-end">
<button class="btn btn-info last-year" id="last-year"><i class="fas fa-chevron-left"></i></button>
<button class="btn btn-info current-year" id="current-year">Current Year</button>
<button class="btn btn-info next-year" id="next-year"><i class="fas fa-chevron-right"></i></button>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-header bg-inverse">
<h2 class="card-title text-white" id="yearly_training_sessions"></h2>
</div>
<div class="card-body">
<span class="badge chart-label-intervall">Intervall</span>
<span class="badge chart-label-longrun">Longrun</span>
<span class="badge chart-label-speedwork">Speedwork</span>
<span class="badge chart-label-stabilisation">Stabilisation</span>
<span class="badge chart-label-competition">Competition</span>
<span class="badge chart-label-cycling">Cycling</span>
<span class="badge chart-label-swimming">Swimming</span>
<br><br>
<canvas id="yearly_session_stats" width="180" height="180"></canvas>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-header bg-inverse d-flex justify-content-between">
<h2 class="card-title text-white" id="yearly_training_days"></h2>
<h2 class="card-title text-white" id="yearly_injury_days"></h2>
</div>
<div class="card-body">
<div class="d-flex justify-content-evenly" style="margin-bottom: 18px">
<span class="badge chart-label-training">Training</span>
<span class="badge chart-label-injury">Free</span>
<span class="badge chart-label-free">Injury</span>
</div>
<br>
<canvas id="yearly_training_stats" width="180" height="180"></canvas>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-header bg-inverse">
<h2 class="card-title text-white">Shoes</h2>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table">
<tbody id="yearly_shoes_stats">
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
After a lot of researching and reading i found the solution for this problem.
The problem occurs because of the hidden content from the hidden tab, which is realized in Bootstrap via "display: none".
.tab-content > .tab-pane, .pill-content > .pill-pane {
display: none; /* this is the problem */
}
To fix this problem you can use height: 0 and overflow-y: hidden to this . Now the charts will stay in place and no empty random space will created.
.tab-content > .tab-pane:not(.active),
.pill-content > .pill-pane:not(.active) {
display: block;
height: 0;
overflow-y: hidden;
}
It will take a little minute to go through that line by line but some of the problem is sure to be your structure of Rows and Columns. I noticed this in the first few lines - Just inside the tab-content is this...
<div class="tab-pane active" id="Month" role="tabpanel">
<div class="row">
<div class="col-md-12">
<div class="row">
With Bootstrap you're not really meant to have the Row's inside the Column's and I think what's happening is that the un-natural layout of the code is creating errors like the one you see. This answer does not discover where your white-space is coming from but I do suspect that fixing the layout of the code will also fix the white-space.

<a href=''> not working on the other pages

Here's the first page that the a href='' code is working .
moneypro.online/website-design
<div class="row animate-box fadeInUp animated-fast">
<!-- Tabs -->
<div class="gtco-tab-content tab-content animated-fast active fadeIn" data-tab-content="1">
<div class="col-md-6">
<div class="gtco-video gtco-bg gtco-image img-responsive" style="background-image: url(/WebsiteDesign.png); background-size: contain; ">
</div>
</div>
<div class="col-md-6">
<h2>Website Design</h2>
<p>As a business start with an idea, an online identity starts with a design concept. Here at Euro ITS we listen to client business goals and incorporate it within our design. Together with our knowledge of digital marketing, our website designs are: </p>
<div class="row">
<div class="col-md-12">
<h4>✔ Responsive</h4>
</div>
<div class="col-md-12">
<h4>✔ User oriented</h4>
</div>
<div class="col-md-12">
<h4>✔ Search engine ready</h4>
</div>
</div>
<div class="row">
<div class="col-md-4">
<button type="button" class="btn btn-success btn-block">Appointment</button>
</div>
<div class="col-md-4">
<button type="button" class="btn btn-info btn-block">Inquiry</button>
</div>
<div class="col-md-4">
<button type="button" class="btn btn-warning btn-block">Pricing</button>
</div>
</div>
</div>
</div>
</div>
and here is the other page that the a href='' code does not work.
moneypro.online/services
<div class="gtco-tab-content-wrap" style="height: 643px;" data-section="services">
<div class="gtco-tab-content tab-content animated-fast active fadeIn" data-tab-content="1">
<div class="col-md-6 col-sm-12 col-xs-12">
<div class="gtco-video gtco-bg img-responsive" style="background-image: url(assets/public/images/icons/WebsiteDesign.png); background-size: contain; ">
</div>
</div>
<div class="col-md-6 col-sm-12 col-xs-12">
<div class="row">
<div class="col-md-12">
<div class="panel">
<div class="panel panel-heading panel-cust-bg">
<h2 class="cust-txt"><strong>Website Design</strong></h2>
</div>
<div class="panel-body">
<div class="row">
<div class="col-md-12">
<p>As a business start with an idea, an online identity starts with a design concept. Here at Euro ITS we listen to client business goals and incorporate it within our design. Together with our knowledge of digital marketing, our website designs are: </p>
</div>
</div>
<div class="row">
<div class="col-md-12">
<h4>✔ Responsive</h4>
</div>
<div class="col-md-12">
<h4>✔ User oriented</h4>
</div>
<div class="col-md-12">
<h4>✔ Search engine ready</h4>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<a href="/contact" class="btn btn-success btn-block" role="button" >Appointment</a>
</div>
<div class="col-md-4">
Inquiry
</div>
<div class="col-md-4">
Pricing
</div>
</div>
</div>
</div>
they're are exactly the same and i don't know why the other page is working . here's the live website of it
live example
Can someone please explain it to me.
Ok, here is the difference between the two:
Working:
<a href="/contact">
<button type="button" class="btn btn-success btn-block">
Appointment
</button>
</a>
Not Working:
<a href="/contact" class="btn btn-success btn-block" role="button">
Appointment
</a>
You are adding class btn to "a" tag, and it thinks the tag is a button so it ignores href attribute.
Every href element needs a corresponding anchor which should be a name or id attribute and it must match/exist in the page.
This example by w3schools show a good practice of href.

Metro tile layout with Bootstrap?

I'm trying to create a tile layout with bootstrap. Here is the website that I would like to try to copy: link
I kind of achieved this by using columns but using margins and paddings will break them. Aforementioned website uses some kind of script to automatically set their position value (e.g., position:absolute;top:0;left:248px). How is this done?
Here is what I have: jsfiddle
<div class="col-lg-4">
<div class="row">
<div class="col-lg-12 metro-1">
<img src="https://placehold.it/600x600/313236/000000" style="width:100%;">
<div class="top-text-block">
<div class="top-text-block-inner">
check out our<br>hottest winit
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-4">
<div class="col-lg-12 metro-2">
<div class="row upper-metro">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<img src="https://placehold.it/300x300/ffffff/000000" style="width:100%;">
<a href="#" class="block-layer">
<div class="block-layer-inner">
</div>
</a>
<div class="bottom-text-block">
<div class="block-name">
Text
</div>
<div class="block-price">
Price
</div>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<img src="https://placehold.it/300x300/23AE8F/000000" style="width:100%;">
<a href="#" class="block-layer">
<div class="block-layer-inner">
</div>
</a>
<div class="bottom-text-block">
<div class="block-name">
Text
</div>
<div class="block-price">
Price
</div>
</div>
</div>
</div>
<div class="row lower-metro">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 lower-metro-inner">
<img src="https://placehold.it/600x300/DEDCE1/000000" style="width:100%;">
<div class="center-text-block">
some text goes here
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-4">
<div class="col-lg-12 metro-3">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<div class="col-lg-12">
<img src="https://placehold.it/300x300/5B2988/000000" style="width:100%;">
<a href="#" class="block-layer">
<div class="block-layer-inner">
</div>
</a>
<div class="bottom-text-block">
<div class="block-name">
Text
</div>
<div class="block-price">
Price
</div>
</div>
</div>
<div class="col-lg-12">
<img src="https://placehold.it/300x300/C64001/000000" style="width:100%;">
<a href="#" class="block-layer">
<div class="block-layer-inner">
</div>
</a>
<div class="bottom-text-block">
<div class="block-name">
Text
</div>
<div class="block-price">
Price
</div>
</div>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 right-metro">
<img src="https://placehold.it/300x600/017B39/000000" style="width:100%;">
<div class="center-text-block">
some text goes here
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-4">
<div class="col-lg-12 metro-4">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<div class="col-lg-12">
<img src="https://placehold.it/300x300/5535B1/000000" style="width:100%;">
<a href="#" class="block-layer">
<div class="block-layer-inner">
</div>
</a>
<div class="bottom-text-block">
<div class="block-name">
Text
</div>
<div class="block-price">
Price
</div>
</div>
</div>
<div class="col-lg-12">
<img src="https://placehold.it/300x300/7EC0BF/000000" style="width:100%;">
<a href="#" class="block-layer">
<div class="block-layer-inner">
</div>
</a>
<div class="bottom-text-block">
<div class="block-name">
Text
</div>
<div class="block-price">
Price
</div>
</div>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<div class="col-lg-12">
<img src="https://placehold.it/300x300/E4A706/000000" style="width:100%;">
<a href="#" class="block-layer">
<div class="block-layer-inner">
</div>
</a>
<div class="bottom-text-block">
<div class="block-name">
Text
</div>
<div class="block-price">
Price
</div>
</div>
</div>
<div class="col-lg-12">
<img src="https://placehold.it/300x300/925D63/000000" style="width:100%;">
<a href="#" class="block-layer">
<div class="block-layer-inner">
</div>
</a>
<div class="bottom-text-block">
<div class="block-name">
Text
</div>
<div class="block-price">
Price
</div>
</div>
</div>
</div>
</div>
</div>
</div>
It was called Mansory layout. I didn't know what it was called but now I got it. Funny how things are so easy when you actually know what to search for. Haha!

How can I put the icon and tag in the same line?

The icon and the word hi can not be put into the same line
<link href="http://code.ionicframework.com/1.3.1/css/ionic.css" rel="stylesheet"/>
<div class="row">
<div class="col">
<h5 class="title">hi</h5>
</div>
<i class="icon ion-ios-heart"></i>
</div>
Thanks!!
Method 1 - CSS
<div class="row">
<div class="col">
<span class="item">
<h5 style="display:inline" class="title">hi</h5>
<i class="icon ion-ios-heart"></i>
</span>
</div>
</div>
Method 2 - In same column
<div class="row">
<div class="col">
<h5 class="title">hi<i class="icon ion-ios-heart"></i></h5>
</div>
</div>
Method 3 - In two columns in same row
<div class="row">
<div class="col">
<h5 class="title">hi</h5>
</div>
<div class="col">
<i class="icon ion-ios-heart"></i>
</div>
</div>
h5 will display as a block, so it has its own line.
Should work with the icon inside the h5 tags, though.

Filter visible/hidden bootstrap columns using buttons

Any JQuery assistance to show/hide bootstrap columns on click buttons?
<div class="btn-group" role="group" aria-label="...">
<button type="button" class="btn btn-default">Category1</button>
<button type="button" class="btn btn-default">Category2</button>
<button type="button" class="btn btn-default">Category3</button>
</div>
This should control visible column/s in animated transition
<div class="row">
<!-- Category1 -->
<div class="col-lg-4">
...
</div>
<!-- Category2 -->
<div class="col-lg-4">
...
</div>
<!-- Category2 -->
<div class="col-lg-4">
...
</div>
</div>
Try with JQuery to toggle hide/show on button click for respective category DIV.
JQuery toggle() function toggle the display property of the element.
$(document).ready(function() {
$('.btn-group').on('click', 'button', function() {
var $btn = $(this);
var categoryId = $btn.data('categoryId');
$('#' + categoryId).toggle();
});
});
Respective id of category div is added as data attribute on each button.
Complete sample code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('.btn-group').on('click', 'button', function() {
var $btn = $(this);
var categoryId = $btn.data('categoryId');
$('#' + categoryId).toggle();
});
});
</script>
<div class="btn-group" role="group" aria-label="...">
<button type="button" class="btn btn-default" data-category-id="Category1">Category 1</button>
<button type="button" class="btn btn-default" data-category-id="Category2">Category 2</button>
<button type="button" class="btn btn-default" data-category-id="Category3">Category 3</button>
<div class="row">
<!-- Category1 -->
<div class="col-lg-4" id="Category1">Category 1</div>
<!-- Category2 -->
<div class="col-lg-4" id="Category2">Category 2</div>
<!-- Category3 -->
<div class="col-lg-4" id="Category3">Category 3</div>
</div>
</div>
I could find Codepen sample using Isotope. Just the way I needed.
<div id="filters" class="button-group">
<button class="btn btn-primary" data-filter="*">show all</button>
<button class="btn btn-primary" data-filter=".web">WEB</button>
<button class="btn btn-primary" data-filter=".design">DESIGN</button>
</div>
<div class="container-fluid no-gutter">
<div id="posts" class="row">
<div id="1" class="item web col-sm-3">
<div class="item-wrap">
<img class="img-responsive" src="//lorempixel.com/600/600">
</div>
</div>
<div id="2" class="item web col-sm-3">
<div class="item-wrap">
<img class="img-responsive" src="//lorempixel.com/600/600/nature">
</div>
</div>
<div id="3" class="item design col-sm-3">
<div class="item-wrap">
<img class="img-responsive" src="//lorempixel.com/600/600/people/1">
</div>
</div>
<div id="4" class="item design col-sm-3">
<div class="item-wrap">
<img class="img-responsive" src="//lorempixel.com/600/600/technics">
</div>
</div>
<div id="5" class="item web col-sm-3">
<div class="item-wrap">
<img class="img-responsive" src="//lorempixel.com/600/600/transport/1">
</div>
</div>
<div id="6" class="item design col-sm-3">
<div class="item-wrap">
<img class="img-responsive" src="//lorempixel.com/600/600/sports">
</div>
</div>
<div id="7" class="item web col-sm-3">
<div class="item-wrap">
<img class="img-responsive" src="//lorempixel.com/600/600/business/1">
</div>
</div>
<div id="8" class="item design col-sm-3">
<div class="item-wrap">
<img class="img-responsive" src="//placekitten.com/600/600" title="Can you believe it??">
</div>
</div>
</div>
</div>
Here is the code: http://codepen.io/bmodena/pen/Fybdu

Categories

Resources