I am very new to jQuery and one thing I always wanted to try was to create buttons that show different sections one at a time. I have just learned toggleClass function and currently this is what I have.
There are 3 things I would like to fix to get a desired result. I really appreciate for tips and help. Followings are the problems.
-
I would like to start off with lists in the section being visible and users are given a choice to toggle between whichever they want to see. Does this mean I have to apply display:”none” and probably name it inactive class and play around with it?
-
Every time I click the same button again, it goes back to the original state which is “display:none”. Is there a way to just stop the toggle effect when it is pressed for the second time?
-
If I play around with the first 3 buttons and try to show all by pressing AllButton, it does not show the SEO part for some reason. I assume it has something do with how I coded…
If what I wrote down confuses you even more, could you help me find a reference or link that teaches how to do this in a proper way? I can’t seem to find anywhere including YouTube and CodePen. I will also leave a portfolio link that has a example of what I am trying to achieve. Thank you for reading!
https://evelyncranston.ca/
var WebButton = $(".buttons button:nth-child(1)");
var EButton = $(".buttons button:nth-child(2)");
var SEOButton = $(".buttons button:nth-child(3)");
var AllButton = $(".buttons button:nth-child(4)");
WebButton[0].onclick = function(){
$("#points-of-sale ul li:nth-child(2)").removeClass("active");
$("#points-of-sale ul li:nth-child(3)").removeClass("active");
$("#points-of-sale ul li:nth-child(1)").toggleClass("active");
}
EButton[0].onclick = function(){
$("#points-of-sale ul li:nth-child(1)").removeClass("active");
$("#points-of-sale ul li:nth-child(3)").removeClass("active");
$("#points-of-sale ul li:nth-child(2)").toggleClass("active");
}
SEOButton[0].onclick = function(){
$("#points-of-sale ul li:nth-child(1)").removeClass("active");
$("#points-of-sale ul li:nth-child(2)").removeClass("active");
$("#points-of-sale ul li:nth-child(3)").toggleClass("active");
}
AllButton[0].onclick = function(){
$("#points-of-sale ul li").toggleClass("active");
}
#points-of-sale ul li {
display:none;
}
.active {
display:block !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="buttons">
<button>Web Design</button>
<button>E-Commerce</button>
<button>SEO</button>
<button>All</button>
</div>
<section id="points-of-sale">
<ul>
<li>
<h3>Web Design</h3>
<p>quatis alique mos et aut occae cum, veliquaspit quo quam, si idem reprorisqui doluptatur accum si sunt ut officiisto enecab id et aut es et laboribusam endi rerum as minullorent officiatum non cuscium quuntem</p>
</li>
<li>
<h3>E-commerce</h3>
<p>quatis alique mos et aut occae cum, veliquaspit quo quam, si idem reprorisqui doluptatur accum si sunt ut officiisto enecab id et aut es et laboribusam endi rerum as minullorent officiatum non cuscium quuntem</p>
</li>
<li>
<h3>SEO</h3>
<p>quatis alique mos et aut occae cum, veliquaspit quo quam, si idem reprorisqui doluptatur accum si sunt ut officiisto enecab id et aut es et laboribusam endi rerum as minullorent officiatum non cuscium quuntem</p>
</li>
</ul>
</section>
<!-- begin snippet: js hide: false console: true babel: false -->
4
Answers
Instead of
toggleClass
useaddClass
to prevent being hide the section when we click button twice.Shorter version:
https://jsfiddle.net/ewu019hj/1/
Try this:
https://jsfiddle.net/ewu019hj/
It encapsulates your hide/logic into one function which the button handlers can call.
This also gives you the option to specify a default.
use this code :
It’s a simple logic see code here
I am just set a class ‘current’ in last button where click user to show all items.
Description: ‘window load event’ select the first button and set ‘current’ class and show the list first item with simple animation.
when a user click the active item our not needed any action
when click the all item show button then the show all item and add button class ‘current’ and remove other button class ‘current’
and get button index and show the content here.
Thank you