I'm using JS code on my WP website to make submenu, in the main nav, open on click (and not on hover).
But when I click on an "empty" parent link, I go back to the top of my page. I know I should use something like e.preventDefault(); but I cant' figure out how to fix my problem (and I'm not good with JS hahaha).
My html code menu looks like this (I can't modify because it is generated by WP) :
<ul class="menu">
<li class="menu-item-has-children"><a href="#">Item #1</a>
<ul class="sub-menu">
<li><a href="http://www.example.com/sublink1/">Sub-link #1</a></li>
<li><a href="http://www.example.com/sublink2/">Sub-link #2</a></li>
<li><a href="http://www.example.com/sublink3/">Sub-link #3</a></li>
</ul>
</li>
<li><a href="http://www.example.com/item2/">Item #2</a></li>
<li class="menu-item-has-children"><a href="#">Item #3</a>
<ul class="sub-menu">
<li><a href="http://www.example.com/sublink4/">Sub-link #4</a></li>
<li><a href="http://www.example.com/sublink5/">Sub-link #5</a></li>
</ul>
</li>
</ul>
And my JS :
<script type="text/javascript">
jQuery(document).ready(function (e) {
function t(t) {
e(t).bind("click", function (t) {
t.preventDefault();
e(this).parent().fadeOut()
})
}
e(".menu-item-has-children > a").click(function () {
var t = e(this).parents(".menu-item-has-children").children(".sub-menu").is(":hidden");
e(".menu-item-has-children .sub-menu").hide();
e(".menu-item-has-children > a").removeClass("active");
if (t) {
e(this).parents(".menu-item-has-children").children(".sub-menu").toggle().parents(".menu-item-has-children").children(".menu-item-has-children > a").addClass("active")
}
});
e(document).bind("click", function (t) {
var n = e(t.target);
if (!n.parents().hasClass("menu-item-has-children")) e(".menu-item-has-children .sub-menu").hide();
});
e(document).bind("click", function (t) {
var n = e(t.target);
if (!n.parents().hasClass("menu-item-has-children")) e(".menu-item-has-children > a").removeClass("active");
})
});
</script>
Can someone help me ? I thank you in advance :)