admin管理员组文章数量:1435859
I have two toggles with a class .submenu-toggle and they contain a ul child with a class .submenu. What I would like to happen is when the user clicks on .submenu-toggle the ul child(.submenu) of that class(submenu-toggle) is shown and then when it is clicked again it is hidden.
I need to achieve this using pure Javascript without any JQuery.
If you're able to also let me know how to hide .submenu if the user clicks outside that element... that would be awesome!
Thanks for your time and help.
Here is my current Javascript:
// Drop Down Menus
var subToggle = document.getElementsByClassName("submenu-toggle");
var menu = subToggle.children;
for (var i = 0; i < subToggle.length; i++) {
subToggle.item(i).onclick = function () {
menu[1].style.display = "block";
}
}
Here is my current html:
<ul>
<li class="submenu-toggle"><a href="#">Menu 1</a>
<ul class="submenu">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</li>
<li class="submenu-toggle"><a href="#">Menu 2</a>
<ul class="submenu">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</li>
</ul>
I have two toggles with a class .submenu-toggle and they contain a ul child with a class .submenu. What I would like to happen is when the user clicks on .submenu-toggle the ul child(.submenu) of that class(submenu-toggle) is shown and then when it is clicked again it is hidden.
I need to achieve this using pure Javascript without any JQuery.
If you're able to also let me know how to hide .submenu if the user clicks outside that element... that would be awesome!
Thanks for your time and help.
Here is my current Javascript:
// Drop Down Menus
var subToggle = document.getElementsByClassName("submenu-toggle");
var menu = subToggle.children;
for (var i = 0; i < subToggle.length; i++) {
subToggle.item(i).onclick = function () {
menu[1].style.display = "block";
}
}
Here is my current html:
<ul>
<li class="submenu-toggle"><a href="#">Menu 1</a>
<ul class="submenu">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</li>
<li class="submenu-toggle"><a href="#">Menu 2</a>
<ul class="submenu">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</li>
</ul>
Share
Improve this question
asked Nov 8, 2016 at 12:34
DBoiDBoi
6872 gold badges20 silver badges36 bronze badges
0
1 Answer
Reset to default 5You could easily do this menu with CSS. But here's an example with vanilla JavaScript:
// JS
var submenu = document.getElementsByClassName("submenu-toggle");
for (var i = 0; i < submenu.length; i++) {
submenu[i].addEventListener('click', menus, false);
}
function menus() {
var menu = this.querySelector('.submenu');
menu.classList.toggle("hidden");
};
// CSS
.hidden {
display: none;
}
DEMO - Codepen
本文标签: htmltoggle child of clicked class pure Javascriptno JQueryStack Overflow
版权声明:本文标题:html - toggle child of clicked class pure Javascript - no JQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745673875a2669732.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论