I'm trying to translate some jQuery to toggle a button/responsive menu and just can't get my version to work. I know most people are pretty jazzed about jQuery but we aren't allowed to use it in our class projects, so I really need to understand the how/why of my code not working.
jQuery Code:
var content = $('main');
$('button').click(function () {
($('i').hasClass('fa-bars')) ?
$('i').removeClass('fa-bars').addClass('fa-times'):
$('i').removeClass('fa-times').addClass('fa-bars');
$('ul').toggleClass('nav-visible');
content.toggleClass('active');
});
JavaScript Code:
var content = document.querySelector('main');
var button = document.querySelector('button');
function clickListener(button) {
"use strict";
button.addEventListener('click', function () {
var i = document.querySelector('i');
if (i.contains('fa fa-bars') === true) {
i.remove('fa fa-bars');
i.add('fa fa-times');
} else {
i.remove('fa fa-times');
i.add('fa fa-bars');
}
var ul = document.querySelector('ul');
ul.toggleClass('nav-visible');
content.toggleClass('active');
});
}
However, the jQuery equivalent code in JavaScript is not working.