Answer the question
In order to leave comments, you need to log in
How to remake JS so that the submenu opens on clicking on the li area, and not on the button?
Hello.
I'm going here in a simple and not a simple way. I want to make myself a quick site on WP on a ready-made theme from Astra. But there are problems that do not suit me. So far, I've only encountered them in a hat, I hope they won't be there anymore.
1. I’ll start with the one I didn’t cope with: I don’t like that in the mobile version of the menu, in order to open a submenu, you need to click specifically on the button in the form of a checkmark, I want it to open when you click on the entire line, on the area li.
With the help of such a tool, I tracked down the js section that is supposedly responsible for this:
function d(e) {
if (e) {
var t = e.getElementsByTagName("button")[0];
if (void 0 !== t || void 0 !== (t = e.getElementsByTagName("a")[0])) {
var a = e.getElementsByTagName("ul")[0];
if (void 0 !== a) {
a.setAttribute("aria-expanded", "false"), -1 === a.className.indexOf("nav-menu") && (a.className += " nav-menu"), t.onclick = function () {
-1 !== e.className.indexOf("toggled") ? (e.className = e.className.replace(" toggled", ""), t.setAttribute("aria-expanded", "false"), a.setAttribute("aria-expanded", "false")) : (e.className += " toggled", t.setAttribute("aria-expanded", "true"), a.setAttribute("aria-expanded", "true"))
};
for (var n = a.getElementsByTagName("a"), s = a.getElementsByTagName("ul"), o = 0, l = s.length; o < l; o++) s[o].parentNode.setAttribute("aria-haspopup", "true");
for (o = 0, l = n.length; o < l; o++) n[o].addEventListener("focus", L, !0), n[o].addEventListener("blur", L, !0), n[o].addEventListener("click", b, !0)
}
else t.style.display = "none"
}
}
}
-1 !== e.className.indexOf("toggled") ? (e.className = e.className.replace(" toggled", ""), t.setAttribute("aria-expanded", "false"), a.setAttribute("aria-expanded", "false")) : (e.className += " toggled", t.setAttribute("aria-expanded", "true"), a.setAttribute("aria-expanded", "true"))
.menu-item-316 > a {
pointer-events: none;
}
let nav = document.querySelector('#site-navigation');
let ul = document.querySelector('#ast-hf-menu-1');
let click_li = document.querySelector('.menu-item-316');
let button = document.querySelector('.menu-item-316 button');
let sub_ul = document.querySelector('.menu-item-316 ul');
click_li.on('click', function(){
nav.className += "toggled";
ul.attr("aria-expanded", true);
click_li.className += "ast-submenu-expanded";
button.attr("aria-expanded", true);
sub_ul.style.display = 'block';
});
Answer the question
In order to leave comments, you need to log in
For some reason, all the answers were given to me in the comments, so I can’t select the right one.
The answers were only to my 1st question and all the answers are aimed at creating a separate js, no one suggested picking the old one from the topic.
Sergey Sergey , gave a useful answer in a comment, this answer was closest to me.
But it doesn't work. Most likely due to the specificity of already written js from the theme.
But this answer became a study object for me, and I learned more about the use of selectors and a lot of new things. In general, he led me to many questions, the answers of which I found in Google.
As a result, I tried many different approaches and now settled on this code:
$('li.menu-item-has-children').on('click', function() {
//var stl = $('ul', 'li.menu-item-has-children').attr ('style');
var stl = $('li.menu-item-has-children', 'div#ast-mobile-header').children("ul").attr ('style');
if (stl == 'display: none;'){
$('ul', 'li.menu-item-has-children').attr ('style', 'display: block;');
}
else {$('ul', 'li.menu-item-has-children').attr ('style', 'display: none;');}
//return false; //перестаёт работать ссылка в вложенном меню
});
//var stl = $('ul', 'li.menu-item-has-children').attr ('style');
var stl = $('li.menu-item-has-children', 'div#ast-mobile-header').children("ul").attr ('style');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question