Home › Forums › JavaScript › show/hide content on click with js
- This topic is empty.
-
AuthorPosts
-
February 25, 2017 at 4:08 am #251914FunkaholikParticipant
Hey guys .. listen
i saw a lot of methods on how to HIDE/show content on click but
i haven’t found a method how to SHOW/hide content on click((
so obvious thing and no info about it, so i’m moving to js slowly))say you have a clickable menu and some content is hidden by default
you click a bubble to show content .. so what i want is when you click on any other bubble it hide all other content and show only the one that is attached to a particular bubble.
also how to add some transition/animation effects to that click?the only way for now that i’ve figured is to toggle classes but that’s kinda stooped and there should be more proper way to do that i believe.
February 25, 2017 at 5:58 am #251917ShikkedielParticipantHave a look at this…
Still toggles classes though, maybe using
hide()
andshow()
could be more convenient.how to add some transition/animation effects to that click?
What exactly should the effect look like then?
February 25, 2017 at 8:01 am #251938FunkaholikParticipantwheeeeee it’s working!!))
but it searches for class show/hide only in nested elements
what if i want to control any other element with the same bubble?and about animations you see it slowly fades in but don’t wants to fade out.
here is an exampleFebruary 25, 2017 at 8:41 am #251941ShikkedielParticipantsearches for class show/hide only in nested elements
Not exactly true, only the
hide
part… it then adds any element with theshow
class. So even if it is outside the current parent.what if i want to control any other element with the same bubble?
In general jQuery’s ace with this but you’d have to be a bit more specific.
For the fading out, it seems key to not make use of
display: none
.February 25, 2017 at 9:07 am #251942FunkaholikParticipantwell you see in the example there is a div
how to assign any of these buttons to control a div?and display: none; i use to hide a content completly, not just make it transparent
off course i can change z-index or hide it on the outside of the window but display: none; is the best way i assumeFebruary 25, 2017 at 10:57 am #251944ShikkedielParticipantYou could add it with a separate class when the animation has ended:
Another good option is
visibility: hidden
by the way. It is then not clickable but still takes up the physical space.how to assign any of these buttons to control a div?
What’s the relation between them? You’ll see it’s already responding in one way because it has the
show
class.Edit – it’s not very smooth in all cases but at this point it might get more straightforward to do the animations with jQuery as well. Switching classes and event listeners only go so far.
February 25, 2017 at 12:08 pm #251949FunkaholikParticipanthere is our first pen
class .menu_blocks there searching for elements with class .hide and change them to .shownow let’s then make a small reshuffle)
here is a new pen
how to make a same toggling effect as the first one but using only id’s or at least without using a same class as a selector ???
so i can assign to a specific id a specific element within the page and make them relate to each other somehow (meaning id’s)
so when one id is on (content it relates to is visible) other id’s off (other content hidden)February 25, 2017 at 1:51 pm #251959ShikkedielParticipantHere’s an example based on the order they’re in (first button correlates to first div and so on):
But it’s JS so there would be many ways, without any classes even:
Semantically they’re buttons by the way…
February 25, 2017 at 2:31 pm #251962FunkaholikParticipantShikkediel, you literally made my day.
thanx a lot!maaan i gotta learn js=((
P.S. you know what would be even cooler?))
if you would make them divs hidden if click to a free area
(for both examples cause i’m not sure which one is easier to handle in future)February 26, 2017 at 1:21 am #251965ShikkedielParticipantNo problem. :-)
Here’s the basic approach you could take for what you mentioned:
$('button, div').click(function(e) { e.stopPropagation(); }); $(window).click(function() { $('div').removeClass('active'); });
It basically hides the divs on any click, making sure those that occur on the buttons or divs aren’t “heard” (they don’t bubble up). Added to both earlier pens.
maaan i gotta learn js
No doubt you will, it’s definitely worth it.
February 26, 2017 at 2:32 am #251966FunkaholikParticipanti’m actually came up with a new idea already)
Can you add a transition for a content of a button?
Say you click a button and for example 1 changes to X (✖)
you click 2, X turns 1 again and 2 turns into X .. and if you click X all divs become hidden
and X turns back to its original value (1, 2 ,3 etc) .. can you do that ^_^?and about js i can’t find a good break in online crash course
i’v read a bit here and there but its all boring
i like to learn at practice but with js unlike html/css it’s a bit hard to do when you’re not familar with syntax
so i think i gotta take courses or so .. or i’ll be bugging you here forever=))February 26, 2017 at 3:24 am #251967ShikkedielParticipantLet me see if I can figure out what you mean exactly and put something together…
I didn’t really do any course myself, just jumped into it and fiddled around until I understood the basics. Usually motivated by having an idea of what I wanted to create (much like you when I look at what you’ve been posting), followed by a bunch of googling plus trial and error. Might not be the “right” way but in my experience it’s the quickest path to learn stuff because you’re busy with what interested you to begin with.
Not that I’d want to dissuade anyone from taking a good course of course…
February 26, 2017 at 4:58 am #251971ShikkedielParticipantCheck it out:
If I understood correctly…
Using the order of the elements was definitely the way to go on this one.
February 26, 2017 at 6:10 am #251972FunkaholikParticipantyeah but what if i have say not numbers but names for buttons like so
February 26, 2017 at 6:58 am #251977ShikkedielParticipantThat’s a minor adjustment, “saving” the text in an array. But still using the order of the elements:
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.