How do you show an element after a user clicks on a separate element on the page using JQuery?
I have created a side menu with the following HTML:
<li id="navicon">
<a href="" class="nav">
<img src ="/assets/navicon-round.svg/" />
</a>
</li>
<ul class="side-categories">
<li><a href="">CSS</li></a>
<li><a href="">HTML</li></a>
<li><a href="">JS/Jquery</li></a>
<li><a href="">Miscellaneous</li></a>
</ul>
I set .side-categories
to display:none
Now I want to be able show .side-categories
when I click on #nav
I just started learning JQuery and this is what I have come up with. Yet, it is not working. I am hoping that someone can tell me what I have done wrong here.
$( document ).ready(function() {
$('#nav').on('click')event(function()) {
event(showNavBar);
$('.side-categories').show();
});
});
Use any element to open the dropdown menu, e.g. a <button>, <a> or <p> element. Use a container element (like <div>) to create the dropdown menu and add the dropdown links inside it. Wrap a <div> element around the button and the <div> to position the dropdown menu correctly with CSS.
A good trick is to hide the sidebar menu using a class.
In your CSS:
.hide {
display: none;
}
And in your HTML: (the <a>
can be removed if you want as jQuery can detect a click on any element)
<li id="navicon">
<img class="nav" src ="/assets/navicon-round.svg/" />
</li>
<ul class="side-categories hide">
<li><a href="#">CSS</a></li>
<!-- More menu items -->
</ul>
Then in your jQuery:
$(function() {
$('.nav').on('click', function(e) {
e.preventDefault();
$('.side-categories').toggleClass('hide');
});
});
This is a very simple way of allowing the user to hide the side menu again by clicking the same button - which is usually the expected behaviour.
An additional benefit is you keep your functions and styles seperate.
Here is HTML code :
$(function() {
$('.nav').on('click',function(e) {
e.preventDefault();
$(".side-categories").slideToggle();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<li id="navicon">
<a href="" class="nav">
<img style="height:40px;position:absolute;" src ="data:image/gif;base64,R0lGODlhZABkAIABAIeHh////yH/C1hNUCBEYXRhWE1QPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmU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OCgYB/fn18e3p5eHd2dXRzcnFwb25tbGtqaWhnZmVkY2JhYF9eXVxbWllYV1ZVVFNSUVBPTk1MS0pJSEdGRURDQkFAPz49PDs6OTg3NjU0MzIxMC8uLSwrKikoJyYlJCMiISAfHh0cGxoZGBcWFRQTEhEQDw4NDAsKCQgHBgUEAwIBAAAh+QQBAAABACwAAAAAZABkAAACtIyPqcvtD6OctNqLs968+w+G4kiW5omm6sq27gvH8kzX9o3n+s73/g8MCofEovGITCqXzKbzCY1eANSq9YrNardZGvcLDmO94rJ5PDurz+S1+9t+y9Gyuf0qzev3/L7/DxgoOFh3ZxhnKIeY6LbIqOb4aBYpKUZZCUaoucnZ6fkJGio6eoDZmGYKiZo6ucpq6fqaGSvLdVlrRaq7y9vr+wscLDxMXGx8jJysvMzc7PwMHU1aAAA7" />
</a>
</li>
<ul class="side-categories" style="display:none">
<li><a href="">CSS</li></a>
<li><a href="">HTML</li></a>
<li><a href="">JS/Jquery</li></a>
<li><a href="">Miscellaneous</li></a>
</ul>
for Live Demo : https://jsfiddle.net/4zuL5bmt/
Working fiddle.
You should replace :
event(showNavBar);
By :
e.preventDefault();
Full Code :
$(function() {
$('.nav').on('click',function(e) {
e.preventDefault();
$('.side-categories').show();
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With