Satch3000 Satch3000 - 6 months ago 42
Javascript Question

Javascript TypeError: $(...).parent is not a function

I am having this error when I click on the following code:

onclick="tester.removeit(this);"


I get Error:

TypeError: $(...).parent is not a function


Here is the function:

removeit: function(ele) {

$(ele).parent('div').fadeOut();
console.log(this);

},


How can I fix this?

Answer

Sounds like a library conflict, where you're including PrototypeJS or MooTools after including jQuery.

When you do that, only one library can use $ as its main identifier. You can tell jQuery to "release" $ via noConflict:

<script src="jquery.js"></script>
<script>jQuery.noConflict();</script>
<script src="prototypejs.js"></script>

Then in code where you want to use jQuery, use jQuery rather than $:

// ...
removeit: function(ele) {

    jQuery(ele).parent('div').fadeOut();
    console.log(this);

},
// ...

Or wrap all of your code using jQuery in an IIFE that accepts $ as an arg:

(function($) {
    // ...
    removeit: function(ele) {

        $(ele).parent('div').fadeOut();
        console.log(this);

    },
    // ...
})(jQuery);
Comments