0

I am trying to use a jQuery function on a property of my js object, but it's giving me an error "Uncaught TypeError: Cannot read property 'jquery' of undefined". Here is some sample code:

var foo = {
    bar: $('#element'),
    baz: this.bar.find('#childelement'), // <- jQuery .find() not working
    hideBaz: function() {
        this.baz.hide();
    }
}

Not really sure what to do here, any help would be great

1
  • Could you share the full source code, so we can reproduce the same problem? That way we can provide better answer for you Commented Feb 11, 2016 at 1:49

3 Answers 3

2

In order to enable jquery functionalities, you must use $(this) rather than just this. Without using $(this), you will not be able to use any jquery on that object.

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

Comments

2

you should wrap this in jquery :

var foo = {
    bar: $('#element'),
    baz: $(this).bar.find('#childelement'), 
    hideBaz: function() {
        this.baz.hide();
    }
}

Comments

1

To use jQuery you need to use namespace provided by jQuery. ie - either jQuery or $ which are given with jQuery. You can also create your own namespace by using jQuery.noConflict().

If you use them then only jQuery will know you are trying to access its methods with their objects. Something like $(this) or $("yourselector").

So with the above, you can use

var foo = {
    bar: $('#selector'),
    baz: $(this).bar.find('#childSelector'), 
    hideBaz: function() {
        this.baz.hide();
    }
 }

Hope this be of some help.

Happy Learning :)

Comments

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.