0

I've tried a ton of different methods to get the obj var within my plugin to add classes on click instead of hover but nothing seems to work.. obj.click(function() { seems to work with older versions of jQuery like 1.4.2 but not anything newer.. any suggestions would be highly appreciated

thank you!

 (function($) {
$.fn.vivify = function(options) {

    var defaults = {
        animation: 'bounce',
    };

    var options = $.extend(defaults, options);

    return this.each(function() {
        var o = options;

        var obj = $(this);

        var animation = o.animation;

        obj.hover(function() {
         obj.addClass(o.animation);
         obj.addClass('vivify');
        },

        function() {
            obj.removeClass(o.animation);
        });
    })
}
})(jQuery);
JamesBong
  • 69
  • 8
  • 2
    The large `switch` in the middle of your code seems redundant. Why not just `$(this).addClass(animation)`? (I know this doesn't answer your question, but shorter, simpler code is easier to read and debug) – georgebrock Jun 02 '12 at 08:07
  • just a question: why don't you use obj.addClass('bounce'); instead of using $(this).addClass('bounce'); ?? – mfadel Jun 02 '12 at 08:10
  • using the switch and different cases allows me to apply any of the 56 animations like so, animation:flash or animation:tada i don't see how replacing this entire switch with $(this).addClass(animation) would allow me to add more than one animation..? – JamesBong Jun 02 '12 at 15:28
  • nvm! i figured it out!!! thanks so much that is so much more simple! :D now the only issue is that it still won't work on a click function – JamesBong Jun 02 '12 at 15:52

1 Answers1

0

i would try bind or on and direct you to this link: What's the difference between `on` and `live` or `bind`?

and yes, the commenters are right:

obj.on('click', [selector,] function(){
  obj.addClass(o.animation);
});
Community
  • 1
  • 1
mindandmedia
  • 6,800
  • 1
  • 24
  • 33
  • hey thanks! i was able to reduce the size of the plugin as you showed me, i really appreciate that!!! i had no idea it could be done in such a simple manner, i thought i would have to assign an individual case for each animation class, i attempted the click function but was unsuccessful any other suggestion on methods i could try? – JamesBong Jun 02 '12 at 15:56
  • okay! now finally got it to work with obj.bind("click", function(){ only issue now is how do i force it remove the added class after the click occurs, the class currently sticks and won't allow me to animate with click again, if use toggleClass it works but i have to click it twice to activate the animation again after it's been added the first time – JamesBong Jun 02 '12 at 16:19