Hide rows on click with jQuery - javascript

I have a small self made filter bar for a small agenda.
When i click on the filter button i want to hide the rows which are not in the category.
When i click Marketing, it will hide all other categories, and it's the same for the siblings. When i press ALL I want to show all rows again.
I bet there has to be a better way, i just can't figure it out
(function ($) {
$('#FilterBar a').click(function () {
if ($('#FilterBar a').is('.all')) {
$('.row').show();
}
else if ($('#FilterBar a').is('.pauses')) {
$('#Agenda .row:not(.pauses)').hide();
}
else if ($('#FilterBar a').is('.marketing')) {
$('#Agenda .row:not(.marketing)').hide();
}
else if ($('#FilterBar a').is('.sales')) {
$('#Agenda .row:not(.sales)').hide();
}
});
})(jQuery);
.wrapper {
padding: 20px;
}
#FilterBar a {
padding: 5px;
}
#Agenda {
padding: 15px 5px 5px;
}
#Agenda div {
padding: 20px;
background: #e7e7e7;
}
#Agenda div:nth-child(even) {
padding: 20px;
background: #f7f7f7;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
<div id="FilterBar">
All
Pauses
Marketing
Sales
</div>
<div id="Agenda">
<div class="row pauses"><b>Pause:</b> Break at 10:30</div>
<div class="row marketing"><b>Marketing:</b> This is marketing</div>
<div class="row sales"><b>Sales:</b> Sales is important</div>
</div>
</div>

You could do it like this:
(function ($) {
$('#FilterBar a').click(function () {
$('#Agenda .row').hide();
const className = $(this).attr('class');
$('#Agenda .' + (className === 'all' ? 'row' : className)).show();
});
})(jQuery);
.wrapper {
padding: 20px;
}
#FilterBar a {
padding: 5px;
}
#Agenda {
padding: 15px 5px 5px;
}
#Agenda div {
padding: 20px;
background: #e7e7e7;
}
#Agenda div:nth-child(even) {
padding: 20px;
background: #f7f7f7;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
<div id="FilterBar">
All
Pauses
Marketing
Sales
</div>
<div id="Agenda">
<div class="row pauses"><b>Pause:</b> Break at 10:30</div>
<div class="row marketing"><b>Marketing:</b> This is marketing</div>
<div class="row sales"><b>Sales:</b> Sales is important</div>
</div>
</div>

Related

Show hide Div based on toggle Switch

I'm trying to make a toggle switch work, which will show/hide classes based on if it is checked or unchecked. By default i want to show "pay annually" so it will display the annual price, also a text blub further down the page. If i click "pay monthly" it will display the monthly price, and a monthly text blurb further down the page.
I tried to follow some solution, but at the moment all are showing, and nothing toggles. How can i fix this?
link to codepen
function showHide(e) {
const el = e.target;
if (el.checked) {
el.dataset.checked.split(',').forEach(fld => document.getElementById(fld).parentNode.style.display = 'block');
el.dataset.notChecked.split(',').forEach(fld => document.getElementById(fld).parentNode.style.display = 'none' );
} else {
el.dataset.checked.split(',').forEach(fld => document.getElementById(fld).parentNode.style.display = 'none' );
el.dataset.notChecked.split(',').forEach(fld => document.getElementById(fld).parentNode.style.display = 'block');
}
}
Using jQuery :
$(document).ready(function() {
var checkBoxes = $("input[name='toggle']");
toggle();
$("#toggle").click(function() {
toggle();
});
function toggle() {
if (checkBoxes.prop("checked")) {
$('#coreMonthlyText,#coreMonthlyPrice').show('slow');
$('#coreAnnuallyText,#coreAnnuallyPrice').hide('slow');
} else {
$('#coreMonthlyText,#coreMonthlyPrice').hide('slow');
$('#coreAnnuallyText,#coreAnnuallyPrice').show('slow');
}
}
});
.pricing-box {
background: red;
padding: 25px
}
.row {
display: flex;
flex-direction: row;
flex-wrap: wrap;
width: 100%;
}
.column {
display: flex;
flex-direction: column;
flex-basis: 100%;
flex: 1;
}
.toggle-switch {
cursor: pointer;
background-color: gray;
display: inline-block;
border: 0;
padding-left: 0;
padding-right: 0;
}
.toggle-switch input {
display: none;
}
.toggle-switch,
.toggle-switch span {
border-radius: 35px;
border-style: solid;
border-color: transparent;
padding-top: .75rem;
padding-bottom: .75rem;
}
.toggle-switch span {
border-width: 2px;
padding-left: .75rem;
padding-right: .75rem;
}
.toggle-switch input:checked+span+span,
.toggle-switch input+span {
border-color: #444;
background-color: white;
}
.toggle-switch input+span+span,
.toggle-switch input:checked+span {
background-color: transparent;
border-color: transparent;
}
#coreMonthlyText,
#coreMonthlyPrice,
#coreAnnuallyText,
#coreAnnuallyPrice {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="pricing-box">
<div class="row">
<div class="column">
<div class="core">
<h2>Core</h2>
</div>
</div>
<div class="column">
<div id="coreAnnuallyPrice" class="coreAnnuallyPrice">
$2,399/yr<br /> Normally $3,588/yr
</div>
<div id="coreMonthlyPrice" class="coreMonthlyPrice">
$299/pm<br /> first 2 months free
</div>
</div>
</div>
<label for="toggle" class="toggle-switch">
<input class="toggle-button" id="toggle" type="checkbox" name="toggle" data-checked="coreAnnuallyPrice,coreAnnuallyText" data-not-checked="coreMonthlyPrice,coreMonthlyText">
<span>pay annually</span>
<span>pay monthly</span>
</label>
</div>
<p id="coreAnnuallyText" class="center_text big-text coreAnnuallyText">this is a annual text blurb</p>
<p id="coreMonthlyText" class="center_text big-text coreMonthlyText">this is a monthly text blurb.</p>

Make visible, which Accordion Tab is opened

I have this "Accordion Type" Snippet right now. It opens the content below the button on click and closes it again. Now I am looking for a solution to make the state of the button visible to the user e.g if Button 2 is opened, I want to show a "-" indicating that it can be closed with a click. Same with the closed buttons to show a "+". Is this possible? Thanks in advance :)
This is my code right now:
jQuery(function() {
jQuery('.ss_button').on('click', function() {
jQuery('.ss_content').not(jQuery(this).next('.ss_content')).slideUp('fast');
jQuery(this).next('.ss_content').slideToggle('fast');
});
jQuery('.ss_content').eq(1).show();
});
#ss_menu {
width: auto;
}
.ss_button {
background-color: #049132;
border-bottom: 1px solid #FFFFFF;
cursor: pointer;
padding: 10px;
margin-bottom: 5px;
color: #FFFFFF;
}
.ss_content {
background-color: white;
display: none;
padding: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="ss_menu">
<div class="ss_button">Einsteiger</div>
<div class="ss_content">
<p>Test1</p>
</div>
<div class="ss_button">Mittelklasse</div>
<div class="ss_content">
<p>Test2</p>
</div>
<div class="ss_button">High-End</div>
<div class="ss_content">
<p>Test3</p>
</div>
</div>
I've added some changes to your css:
.ss_button:before {
content: "+ ";
}
.ss_button.active:before{
content: "- ";
}
Then we toogle the active class:
jQuery('.ss_button.active').not(jQuery(this)).removeClass('active')
jQuery(this).toggleClass('active');
This way you don't have to change your html.
Demo
jQuery(function() {
jQuery('.ss_button').on('click', function() {
jQuery('.ss_button.active').not(jQuery(this)).removeClass('active')
jQuery(this).toggleClass('active');
jQuery('.ss_content').not(jQuery(this).next('.ss_content')).slideUp('fast');
jQuery(this).next('.ss_content').slideToggle('fast');
});
jQuery('.ss_content').eq(1).show();
});
#ss_menu {
width: auto;
}
.ss_button {
background-color: #049132;
border-bottom: 1px solid #FFFFFF;
cursor: pointer;
padding: 10px;
margin-bottom: 5px;
color: #FFFFFF;
}
.ss_button:before {
content: "+ ";
}
.ss_button.active:before{
content: "- ";
}
.ss_content {
background-color: white;
display: none;
padding: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="ss_menu">
<div class="ss_button">Einsteiger</div>
<div class="ss_content">
<p>Test1</p>
</div>
<div class="ss_button active">Mittelklasse</div>
<div class="ss_content">
<p>Test2</p>
</div>
<div class="ss_button">High-End</div>
<div class="ss_content">
<p>Test3</p>
</div>
</div>
You can add an empty span in your buttons and change the content (+ or -) in the click functions.
Check lines #4 and #6, the same of the code is the same.
jQuery(function() {
jQuery('.ss_button').on('click', function() {
jQuery('.ss_content').not(jQuery(this).next('.ss_content')).slideUp('fast');
jQuery('.ss_button').find('span').text('+');
jQuery(this).next('.ss_content').slideToggle('fast');
jQuery(this).find('span').text('-');
});
jQuery('.ss_content').eq(1).show();
});
#ss_menu {
width: auto;
}
.ss_button {
background-color: #049132;
border-bottom: 1px solid #FFFFFF;
cursor: pointer;
padding: 10px;
margin-bottom: 5px;
color: #FFFFFF;
}
.ss_content {
background-color: white;
display: none;
padding: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="ss_menu">
<div class="ss_button">Einsteiger
<span>+</span></div>
<div class="ss_content">
<p>Test1</p>
</div>
<div class="ss_button">Mittelklasse
<span>+</span></div>
<div class="ss_content">
<p>Test2</p>
</div>
<div class="ss_button">High-End
<span>+</span></div>
<div class="ss_content">
<p>Test3</p>
</div>
</div>

jQuery slider/switcher/tabs

I had no idea how to title this topic, sorry for that. I am building simple shopping cart splited into few steps. The whole thing is supposed to work similar to sliders or well-known tabs.
Let's see the code to make things easier.
$(document).ready(function(){
$('.stepNumber').click(function(e) {
e.preventDefault();
var stepDesc = $(this).next('.stepDesc');
if(!stepDesc.is(':visible')) {
$('.step').removeClass('stepActive');
$(this).parent().addClass('stepActive');
}
var val = parseInt($('.step.stepActive').children('div.stepNumber').text());
switch(val) {
case 1:
$('.formStepTwo').hide();
$('.formStepOne').show();
break;
case 2:
$('.formStepOne').hide();
$('.formStepTwo').show();
break;
case 3:
alert('blabla');
break;
}
});
});
.formStep {
display: none;
}
step {
float: left;
border: 1px solid #333;
margin-right: 5px;
}
.stepNumber {
background: #333;
color: #fff;
float: left;
padding: 6px 10px;
}
.stepDesc {
text-align: left;
padding: 6px 10px;
width: 150px;
display: none;
}
.stepActive > .stepDesc {
display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="step stepOne stepActive">
<div class="stepNumber">1</div>
<div class="stepDesc">Cart</div>
</div>
<div class="step stepTwo">
<div class="stepNumber">2</div>
<div class="stepDesc">Client data</div>
</div>
<div class="step stepThree">
<div class="stepNumber">3</div>
<div class="stepDesc">Shipping data</div>
</div>
<div class="formStep formStepOne">
Something - tab content
</div>
<div class="formStep formStepTwo">
Something else
</div>
Sorry for the appearance, some cosmetic css is missing here.
The actual problem:
I want to add button "Next" just under my "tab content". If we're in the step 1 at this moment, clicking that button should take us to step 2 and so on.
In the meantime time our "tab menu" script should be activated - as in the snippet: close description of others step, show current description and add class "stepActive".
Almost the same thing you can find in common sliders: arrows (next, prev) to move between slides and also "dot menu" with correct dot being highlighted.
If I'm not mistaken, it's something like that, what you wanted, isn't it?
$(document).ready(function(){
$('.stepNumber:contains(3)').click(function(){
alert('blabla');
});
$('.nextStep').click(function (e) {
e.preventDefault();
var stepActive = $('.stepActive');
if (stepActive.next('.step').length)
stepActive.next().children('.stepNumber')[0].click();
else
$('.step > .stepNumber')[0].click();
});
$('.stepNumber').click(function(e) {
e.preventDefault();
var obj = $(this);
var parent = obj.parent();
var stepDesc = obj.next('.stepDesc');
if(!stepDesc.is(':visible')) {
$('.step').removeClass('stepActive');
parent.addClass('stepActive');
}
$('[id^=step]').hide();
$('#step' + obj.text()).show();
});
$('.stepNumber')[0].click();
});
.formStep {
display: none;
}
.step {
float: left;
border: 1px solid #333;
margin-right: 5px;
}
.stepNumber, .nextStep {
background: #333;
color: #fff;
float: left;
padding: 6px 10px;
}
.stepDesc {
text-align: left;
padding: 6px 10px;
width: 150px;
display: none;
}
.stepActive > .stepDesc {
display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="step">
<div class="stepNumber">1</div>
<div class="stepDesc">Cart</div>
</div>
<div class="step">
<div class="stepNumber">2</div>
<div class="stepDesc">Client data</div>
</div>
<div class="step">
<div class="stepNumber">3</div>
<div class="stepDesc">Shipping data</div>
</div>
<div id="step1" class="formStep">
Something - tab content
</div>
<div id="step2" class="formStep">
Something else
</div>
<div id="step3" class="formStep">
The last one
</div>
<div class="nextStep">Next</div>

Swapping an image when a toggle has been clicked on

I have the following code where I added a plus symbol to one of my service titles. I was informed by someone that when that service is clicked on I should have a minus sign take its place to show that it can be minimized. I am unsure of how to swap out the plus sign when the description has been expanded. Does anyone have any ideas of how I could do that?
Here is a snippet. Click on one of the service names to see the description expand.
$('.service_wrapper').click(function() {
var thisDescription = $('.service_description', $(this));
// Hide all other descriptions
$('.service_description').not(thisDescription).hide();
// Toggle (show or hide) this description
thisDescription.slideToggle(500);
});
.service_wrapper {
border: 1px solid black;
margin: 15px;
width: 20%;
}
.service_list {
margin-left: 20%;
}
.service_title {
padding: 15px 12px;
margin: 0;
font-weight: bold;
font-size: 1em;
}
.service_title:hover {
background-color: gray;
color: blue;
cursor: pointer;
}
.service_description {
display: none;
padding: 8px 14px;
width: 100%;
margin-top: 10px;
font-size: .9em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="service_list">
<div class="service_wrapper">
<div class="service_title">
<img src="http://realtorcatch.com/icons/plusSymbol.png" alt="Service" style="width:10px;height:10px;">Floors</div>
<div class="service_description">The best floors!</div>
</div>
<div class="service_wrapper">
<div class="service_title">Roofs</div>
<div class="service_description">Your roof will be perfect!</div>
</div>
<div class="service_wrapper">
<div class="service_title">Siding</div>
<div class="service_description">mmmm siding.</div>
</div>
<div class="service_wrapper">
<div class="service_title">Paint</div>
<div class="service_description">Fabulous paint!</div>
</div>
<div class="service_wrapper">
<div class="service_title">Kitchen Remodels</div>
<div class="service_description">Pretty kitchen.</div>
</div>
</div>
Here is the working example, i change a little de html and Js
$('.service_wrapper').click(function() {
var thisDescription = $('.service_description', $(this));
var t = $(this);
if(t.hasClass('open'))
{
t.removeClass('open');
t.find('.status').html("+");
}else {
t.addClass('open');
t.find('.status').html("-");
}
// Hide all other descriptions
$('.service_description').not(thisDescription).hide();
// Toggle (show or hide) this description
thisDescription.slideToggle(500);
});
the working example
I'd suggest simply toggling a class to achieve this.
You can add the icon as a background image of a pseudo element inserted into the .service_title element. Then you can simply toggle a class in order to change the icon. Update the background image URLs accordingly. See the updated example for the modified jQuery; it's still only 5 lines.
The relevant CSS:
.service_title:before {
content: '';
background: url('http://i.stack.imgur.com/GC7i2.png') 0 0 / 10px 10px no-repeat;
width: 10px;
height: 10px;
display: inline-block;
vertical-align: middle;
}
.closed .service_title:before {
background-image: url('http://i.stack.imgur.com/ma4L4.png');
}
Updated Example:
$('.service_wrapper').click(function() {
var thisDescription = $('.service_description', $(this));
$('.service_description').not(thisDescription).hide().parent().removeClass('closed');
thisDescription.slideToggle(500).parent().toggleClass('closed');
});
.service_wrapper {
border: 1px solid black;
margin: 15px;
width: 20%;
}
.service_list {
margin-left: 20%;
}
.service_title {
padding: 15px 12px;
margin: 0;
font-weight: bold;
font-size: 1em;
}
.service_title:before {
content: '';
background: url('http://i.stack.imgur.com/GC7i2.png') 0 0 / 10px 10px no-repeat;
width: 10px;
height: 10px;
display: inline-block;
vertical-align: middle;
}
.closed .service_title:before {
background-image: url('http://i.stack.imgur.com/ma4L4.png');
}
.service_title:hover {
background-color: gray;
color: blue;
cursor: pointer;
}
.service_description {
display: none;
padding: 8px 14px;
width: 100%;
margin-top: 10px;
font-size: .9em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="service_list">
<div class="service_wrapper">
<div class="service_title">Floors</div>
<div class="service_description">The best floors!</div>
</div>
<div class="service_wrapper">
<div class="service_title">Roofs</div>
<div class="service_description">Your roof will be perfect!</div>
</div>
<div class="service_wrapper">
<div class="service_title">Siding</div>
<div class="service_description">mmmm siding.</div>
</div>
<div class="service_wrapper">
<div class="service_title">Paint</div>
<div class="service_description">Fabulous paint!</div>
</div>
<div class="service_wrapper">
<div class="service_title">Kitchen Remodels</div>
<div class="service_description">Pretty kitchen.</div>
</div>
</div>
You could just change it within your click binding...
Let's say your using images, just add a data-attribute you can query when you need to, like this...
HTML
<div class="service_wrapper">
<img data-state="plus" class="state" src="plus.png" alt="More"/>
<div class="service_title">Paint</div>
<div class="service_description">Fabulous paint!</div>
</div>
JS
$('.service_wrapper').click(function() {
var state = $(this).find('.state');
if(state.data('state') == 'plus')
state.attr({ 'src': 'minus.png', 'alt': 'Less' }).data('state', 'minus');
else
state.attr({ 'src': 'plus.png', 'alt': 'More' }).data('state', 'plus');
});

how to finalize the spoiler

There is a code http://jsfiddle.net/VWCnd/5/. How to add the indicators ↓ ↑.
Example image http://i.stack.imgur.com/OxpaP.png
Js:
$(document).ready(function(){
$('.spoiler_title').click(function(){
$(this).parent().children('div.spoiler_toggle').toggle();
return false;
});
});
html:
<div>
Title
<div class="spoiler_toggle">
<div class="spoiler_bg">Body</div>
</div>
</div>
CSS:
.spoiler_title {
display:block;
background-color: #551A8B;
color: #fff;
padding: 10px 10px;
}
.spoiler_toggle {
display:none;
}
.spoiler_bg {
background: #9C6AC9;
padding: 5px 5px;
}
Thanks.
You could always do something like this:
http://jsfiddle.net/VWCnd/7/
$('.spoiler_title').click(function(){
var $this = $(this),
visible = $this.parent().children('div.spoiler_toggle').toggle().is(':visible');
$this.find('.spoiler_indicator').html(visible? '↑' : '↓');
return false;
});
HERE IS THE DEMO
CODE -
JS:
$(document).ready(function(){
$('.spoiler_title').click(function(){
$(this).find(".sp_but").text(($(this).find(".sp_but").text() == '↓' ? '↑' : '↓'))
$(this).parent().children('div.spoiler_toggle').toggle();
return false;
});
});
HTML:
<div>
Title <span class="sp_but">↓</span>
<div class="spoiler_toggle">
<div class="spoiler_bg">Body</div>
</div>
</div>
CSS:
.spoiler_title {
display:block;
background-color: #551A8B;
color: #fff;
padding: 10px 10px;
}
.spoiler_toggle {
display:none;
}
.sp_but{
float: left;
margin-right:20px;
}
.spoiler_bg {
background: #9C6AC9;
padding: 5px 5px;
}

Categories