0

As I know now .toggle() method is deprecated in jQuery version 1.9. It is ok, but I was familiar with it. The solution was to import the latest jQuery migrate plugin.

On the official jQuery API documentation I can see that there is a note in the description of the .toggle() method:

Not recommended. Use .toggleClass() instead

Yes, I may use .toggleClass() instead, but in this case it only switches between two classes.

Also, why is it not recommended to use?

Max Krizh
  • 585
  • 3
  • 7
  • 34
  • You can easily create your own toggle() -> http://stackoverflow.com/questions/17840644/jquery-toggle-method-is-behaving-wierdly/17840756#17840756 – adeneo Jul 27 '13 at 14:14

2 Answers2

1

The "Not recommended. Use .toggleClass() instead" comment is about the provided example, which toggles a class using .toggle(), it's not a general comment about .toggle().

The docs also state why it is not recommended to use:

The .toggle() method is provided for convenience. It is relatively straightforward to implement the same behavior by hand, and this can be necessary if the assumptions built into .toggle() prove limiting. For example, .toggle() is not guaranteed to work correctly if applied twice to the same element. Since .toggle() internally uses a click handler to do its work, we must unbind click to remove a behavior attached with .toggle(), so other click handlers can be caught in the crossfire. The implementation also calls .preventDefault() on the event, so links will not be followed and buttons will not be clicked if .toggle() has been called on the element.

See further discussion here

antishok
  • 2,910
  • 16
  • 21
0

Because in future versions of jQuery .toggle() won't be there for you to call and will error out in your code.

Joshua Wilson
  • 2,546
  • 1
  • 20
  • 28