0

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.

2
  • where is clickListener called? I only see its declaration Commented Apr 1, 2016 at 5:57
  • @Sindico - I don't think I call the clickListener. Commented Apr 1, 2016 at 19:28

1 Answer 1

2
  1. Use querySelector to select an element
  2. addEventListner to bind event
  3. classList API to add, remove, has and toggle a class.

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');
    // This is same as
    // $('i').toggleClass('fa-times fa-bars');


    $('ul').toggleClass('nav-visible');
    content.toggleClass('active');
});

Equivalent JavaScript Code:

var content = document.querySelector('main'); // Same as $('main')

// bind click event on first <button> element
document.querySelector('button').addEventListener('click', function() {

    // Toggle classes
    document.querySelector('i').classList.toggle('fa-bars');
    document.querySelector('i').classList.toggle('fa-times');

    document.querySelector('ul').classList.toggle('nav-visible');
    content.classList.toggle('active');
}, false);

Demo

Sign up to request clarification or add additional context in comments.

3 Comments

Note: this will only work if you only have one <main>, one <button>, one <i>, one <ul>. jQuery makes you not worry about plurality; if you have multiple of any of those, you'd need to use querySelectorAll and iteration. It also makes some reasonable assumptions about .fa-bars and .fa-times being complementary, and untouched by any other manipulation.
@Tushar I tried your code and it still isn't working. I have a code pen with the jQuery and one with the JS. I think based on a comment above i still need to call the clickListener but haven't implemented it correctly. Nav Reveal (JS) codepen.io/aaknitta/pen/MyEoLa Nav Reveal (JQuery) codepen.io/aaknitta/pen/aNLyLq
@agaluser i've updated answer. Check this demo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.