All things Shopify and commerce
Hello!
I am trying to get the mobile menu to auto-close after clicking on a link rather than clicking on the X to close it manually.
Can you help me with some code and where should I insert it? I have the dawn theme and gempages (one product store/page)
the site: alphapills.ma
pass: paobaw
thank you
Solved! Go to the solution
This is an accepted solution.
Update the code
<script>
document.querySelectorAll('.menu-drawer__menu a').forEach(function(menuLink) {
menuLink.addEventListener('click', function() {
const detailsContainer = document.querySelector('.menu-drawer-container span');
if (detailsContainer) {
detailsContainer.click();
}
});
});
</script>
Hello @pliskin ,
Edit theme.liquid
Search for </body> now here just before of it add tis code:
<script>
document.querySelectorAll('a').forEach(function(menuLink) {
menuLink.addEventListener('click', function() {
const detailsContainer = document.querySelector('.menu-drawer-container span');
if (detailsContainer) {
detailsContainer.click();
}
});
});
</script>
If you need further help don't hesitate to contact me.
You can find the email in the signature below.
Thanks
first thank you for the reply ,i copy past the code you give into theme.liquid as you say. (screenshot) the problem is the same, in mobile when i click a link in menu (mobile) the menu stay open (he must close automatically).
I checked the source code but there is no code there as you shared in the screenshot.
Check the source code in frontend using ctrl + U
and search for the code you added. If found let me know so I can check what you did.
i can't undestand, in shopify it shown (screen1) but in crl+u it'es missing (screen2). i don't know if there is relation but i add 3 code to css.base to remove cart/search/log button from menu
css is a different thing. We cannot use JS code in css files.
btw if you have a JS file use this code there.
Or check which theme template is active and use this code there.
Keep note code will not work until it will reflect in the live store.
thanks for assistance so much, but i dont understand nothing in coding, where i must check theme template or jf file ? (i use gempages, one page) . i must search in this list (screenshot) ??
i randomly add the code in themes.gempages.header.liquid and .... it works. thank you
hello again @Guleria , unfortunately another problem appear: when i click shop now (commander maintenant) the menu open itself automatically... Normally all button named buy now (commander maintenant) in my page redirect to the section of product payment (cod form) but now when i click button buy now, the menu appear automatically. actually the menu appear when i click on any button (like buttons in footer) .
This is an accepted solution.
Update the code
<script>
document.querySelectorAll('.menu-drawer__menu a').forEach(function(menuLink) {
menuLink.addEventListener('click', function() {
const detailsContainer = document.querySelector('.menu-drawer-container span');
if (detailsContainer) {
detailsContainer.click();
}
});
});
</script>
Thank you so much, work perfectly.
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024