- This topic has 15 replies, 5 voices, and was last updated 1 year, 8 months ago by
Fernando.
-
AuthorPosts
-
December 27, 2020 at 9:05 pm #1596215
Henry
What I am trying to achieve is when a button is toggled, then a Grid (div) is opened and closed.
This has been answered here, but I wasn’t able to get it to work…
The CSS and the Jquery are 100% there…
Can anyone spot what is wrong with my logic here?
This is the Grid (with CSS class applied)
This is the Button – and I added the CSS at the button AND div level just to test it, neither worked…
And here:
Thanks for all your help!
I am sure it is a simple thing?
December 27, 2020 at 9:18 pm #1596218Elvin
StaffCustomer SupportHi,
I’ve checked your site and it has script related errors on its console.
WordPress requires
jQuery
instead of$
. Try replacing all of the$
in your script tojQuery
.Example: (taken from the post you’re referencing, replaced all of the
$
tojQuery
).jQuery( '.toggle-heading' ).on( 'click', function( e ) { e.preventDefault(); jQuery( '.toggle-text' ).removeClass( 'show-text' ); jQuery( this ).next( '.toggle-text' ).toggleClass( 'show-text' ); } );
December 28, 2020 at 1:07 am #1596366Henry
Thanks, @Elvin, however, it’s definitely not quite there. Would you mind taking a look? For some reason, the button toggles the grid (via a “div”) but it arranges the icons in a single column?
When I remove all the JQuery it reverts to how it is meant to.
Also, I’d like the button to be Show AND Hide – at the moment it just “shows” and then remains open.
(PS I understand if this is beyond the scope of this support, I am just curious to see if you have an answer).
December 28, 2020 at 1:29 am #1596394Elvin
StaffCustomer SupportCan I offer a different approach?
You can keep everything else but let’s use pure vanilla JS so we don’t have to bother about jQuery libraries.
Try this script instead:
var toggleBtn = document.querySelector('.toggle-heading'); var toggleTgt = document.querySelector('.toggle-text'); toggleBtn.addEventListener("click", function() { toggleTgt.classList.toggle('show-text'); });
Here’s a demo of it in action: https://share.getcloudapp.com/5zudjj7b
December 28, 2020 at 2:24 am #1596461Henry
Works great thanks very much @elvin
One last thing though – any idea why the HTML/GeneratePress layout goes to the single-column format?
I arranged the icons in a grid with x6 columns…
I’ve tried to view the source and use the inspector tool but I can’t seem to find where the issue could be
December 28, 2020 at 2:30 am #1596466Elvin
StaffCustomer SupportAh that’s because you’re toggling from
display: none;
todisplay: block;
instead ofdisplay: flex;
That said, change this CSS:
.toggle-text.show-text { display: block; }
to this
.toggle-text.show-text { display: flex; }
It should display as you intended. (2×6)
December 28, 2020 at 2:39 am #1596474Henry
December 28, 2020 at 3:29 pm #1597472Elvin
StaffCustomer SupportNo problem. Glad it works for you. ๐
March 11, 2022 at 9:22 am #2151034CPWeb
March 12, 2022 at 8:28 pm #2152542Tom
Lead DeveloperLead DeveloperHi there,
It really depends on your HTML structure.
I like using
closest()
to find the parent and then search for the toggle content inside.<div class="container"> <button>Toggle</button> <div>Content</div> </div>
var toggleBtn = document.querySelector('button'); toggleBtn.addEventListener("click", function( event ) { const parent = event.target.closest( 'div' ); const target = parent.querySelector( 'div' ); target.classList.toggle('show-text'); });
March 14, 2022 at 2:36 am #2153665CPWeb
Hello,
Thanks for your reply.
This is the HTML structure (that is present many time in the same page):
<div class="gb-inside-container"> <div class="gb-container toggle-heading"> <p class="gb-headline gb-headline-text">Block title</p> <span class="gb-button gb-button-text">More</span> </div> <div class="gb-container toggle-content"> <p class="gb-headline gb-headline-text">Block content</p> </div> </div>
Here is some CSS:
.toggle-content { display: none; } .toggle-content.show-content { display: flex; }
And the JS (with jQuery):
jQuery( '.toggle-heading' ).on( 'click', function( e ) { e.preventDefault(); jQuery( this ).next( '.toggle-content' ).toggleClass( 'show-content' ); } );
For the moment, only that solution works.
March 14, 2022 at 11:06 pm #2154866Fernando Customer Support
Hi there,
With your current structure, going by your logic, using this Vanilla JS code should work as well:
<script> var toggleBtn = document.querySelector('.toggle-heading'); var toggleTgt = document.querySelector('.toggle-content'); toggleBtn.addEventListener("click", function() { toggleTgt.classList.toggle('show-text'); }); </script>
Here is a test of the code working from my Test website: https://share.getcloudapp.com/L1uRz72Q
Hope this helps! Kindly let us know how it goes. ๐
If this doesn’t work, may we know how you’re adding your JS code? From my end, I’m adding it through a Hook Element hooked to wp_footer as such: https://share.getcloudapp.com/xQuq9W5g
March 15, 2022 at 12:59 am #2154941CPWeb
Hi,
Yes that code works well, but just for the first occurence of my toggle block.
My problem is that I have many occurences of that block in my page:<div class="gb-inside-container"> <div class="gb-container toggle-heading"> <p class="gb-headline gb-headline-text">Block title 1</p> <span class="gb-button gb-button-text">More</span> </div> <div class="gb-container toggle-content"> <p class="gb-headline gb-headline-text">Block content 1</p> </div> </div> <div class="gb-inside-container"> <div class="gb-container toggle-heading"> <p class="gb-headline gb-headline-text">Block title 2</p> <span class="gb-button gb-button-text">More</span> </div> <div class="gb-container toggle-content"> <p class="gb-headline gb-headline-text">Block content 2</p> </div> </div> <div class="gb-inside-container"> <div class="gb-container toggle-heading"> <p class="gb-headline gb-headline-text">Block title 3</p> <span class="gb-button gb-button-text">More</span> </div> <div class="gb-container toggle-content"> <p class="gb-headline gb-headline-text">Block content 3</p> </div> </div>
There, second and third blocks can not be opened when clicking on.
March 15, 2022 at 2:00 am #2154990Fernando Customer Support
If thatโs the case, here is a JS you may try:
<script> document.querySelectorAll('.toggle-heading').forEach(item => { item.addEventListener('click', event => { var placeholder = event.target.closest('div').nextElementSibling; while (placeholder) { if (placeholder.classList.contains('toggle-content')) { placeholder.classList.toggle('show-text'); break; } placeholder = placeholder.nextElementSibling; } }) }) </script>
Hope this helps! ๐
March 15, 2022 at 3:42 am #2155081CPWeb
Thank you. I made few modifications to obtain a good result. That works!
I changed
var placeholder = event.target.closest('div').nextElementSibling;
tovar placeholder = item.nextElementSibling;
.<script> document.querySelectorAll('.toggle-heading').forEach(item => { item.addEventListener('click', event => { var placeholder = item.nextElementSibling; while (placeholder) { if (placeholder.classList.contains('toggle-content')) { placeholder.classList.toggle('show-content'); break; } placeholder = placeholder.nextElementSibling; } }) }) </script>
Thank you very much!
-
AuthorPosts
- You must be logged in to reply to this topic.