Jon Jon - 1 month ago 18
Javascript Question

Jquery is returning prevObject in module

Im using browserify and splitting my code into modules like this article.

I have run into a problem below where

"settings.button"
is not returning the
jQuery object $('.burger')
instead it is returning "prevObject" . Am I missing something here?

any help is appreciated.



'use strict';

var s,
toggleNavigation = {

settings: {
button: $('.burger'),
active: 'is-active'
},

init: function() {
s = this.settings;
this.bind();
},

bind: function() {
s.button.on("click", function() {
s.button.toggleClass(s.active);
});

}

}

module.exports = toggleNavigation




Answer

Probably at the time $('.burger') runs the element doesn't exist yet (not run on document load), so you don't see anything but the prevObject field.

The prevObject field is used for chaining jQuery selectors. It would hold a reference to another DOM element if you would to use something to narrow down the selection, for example: .filter().

Comments