I’m currently using the following block of code to switch between two divs.
$('.btn-my-projects').click(function(e) {
$('.my-projects').show();
$('.all-projects').hide();
});
$('.btn-all-projects').click(function(e) {
$('.my-projects').hide();
$('.all-projects').show();
});
Obviously it works but I’m wondering if there’s a better way to do this. Feels like it could be compressed down to one handler vs. two. If I use delegation I can make it a single handler but it will become longer, requiring a conditional check to see which button was clicked.
Simple:
If you want to get more terse:
Alternately, you could take a completely different approach and put the link between which button shows & hides which div into the markup, using an HTML5
data-*attribute. Something like this:with JavaScript like this:
N.B. in a real page, you would probably want to cache the selected elements.