Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.slideToggle() not working when using Jquery 3.1.1

For some reason I am getting an Uncaught TypeError when I try and execute the following code.

jQuery(function($){
    $(document).ready(function() {
        $(".block").click(function() {
            var currentElement = $(this).find(".form-group");
            currentElement.slideToggle("slow");
        });
    });
});

I get the following error

Uncaught TypeError: currentElement.slideToggle is not a function
at HTMLDivElement.<anonymous> (index.js:6)
at HTMLDivElement.dispatch (jquery-3.1.1.slim.min.js:3)
at HTMLDivElement.q.handle (jquery-3.1.1.slim.min.js:3)

However when I call currentElement.toggle() It works as expected and I do not get any errors.

Can anyone explain why this is?

like image 799
Lenny Avatar asked Feb 05 '23 22:02

Lenny


1 Answers

The problem here is you are using the slim version which excludes the effects like the animation expected from slideToggle; so the function isn't defined. Just switch to the normal version.

From the Jquery Page

Slim build

Sometimes you don’t need ajax, or you prefer to use one of the many standalone libraries that focus on ajax requests. And often it is simpler to use a combination of CSS and class manipulation for all your web animations. Along with the regular version of jQuery that includes the ajax and effects modules, we’ve released a “slim” version that excludes these modules. All in all, it excludes ajax, effects, and currently deprecated code.

like image 144
DaniP Avatar answered Feb 07 '23 11:02

DaniP