0

I've tried to unbind this :

 var foo = $(".js_slide").bind('click' , function () {
  var $contentBlock = $(this).next("li[id^=content_]");

if ($contentBlock.hasClass("is_open")) {
    $contentBlock.animate({
        width: '0%'
    }).removeClass("is_open");
        } 
else {
    // if a content box is open (=> an element with the class .is_open exists), close it
    $(".js_content.is_open").animate({
        width: '0%'
    }).removeClass("is_open");
    $contentBlock.animate({
        width: '45%'        
    }, 500).addClass("is_open");      
}                               
});

With the following piece of code ,but someting goes wrong and i can't figure it out what it could be

$('.js_slide').unbind('click', foo);
Code
  • 83
  • 1
  • 2
  • 10
  • See this answer: http://stackoverflow.com/questions/3972886/how-to-unbind-a-specific-event-handler – robyaw Jun 17 '13 at 12:44
  • Here foo is the matched set of elements returned by ".js_slide" selector. You want to unbind the handler – A. Wolff Jun 17 '13 at 12:44

2 Answers2

1

you should be using on/off rather than bind/unbind as of jquery 1.7.

but for your problem, you are trying to unbind foo which isn't a function in your code. You'd need to do something like this:

var foo = function () {
    var $contentBlock = $(this).next("li[id^=content_]");

    if ($contentBlock.hasClass("is_open")) {
        $contentBlock.animate({
            width: '0%'
        }).removeClass("is_open");
    } else {
        // if a content box is open (=> an element with the class .is_open exists), close it
        $(".js_content.is_open").animate({
            width: '0%'
        }).removeClass("is_open");
        $contentBlock.animate({
            width: '45%'
        }, 500).addClass("is_open");
    }
}

$(".js_slide").bind('click', foo);

then you can unbind like this: $('.js_slide').unbind('click', foo);

Smern
  • 18,746
  • 21
  • 72
  • 90
  • it doesn't work,I'm using it with breakpoint.js.All seems to work fine,except the $('.js_slide').unbind('click', foo); .It's strange – Code Jun 17 '13 at 18:16
  • Are you getting anything in your console? There must be something else because the basic format works well as can be seen here: http://jsfiddle.net/KSutM/ – Smern Jun 17 '13 at 18:25
  • i'm using jquery 1.7.2 ,this can be the problem ? – Code Jun 18 '13 at 22:00
  • Are you seeing anything in your console? It's fine in 1.7.2 http://jsfiddle.net/KSutM/1/. There has to be something else wrong in your code. – Smern Jun 18 '13 at 23:48
0

Do like this...

Basic click

$('div').on('click.name', function(){
    // do something
});

To turn off, use .off()

$('div').off('click.name');

PS : name , name whatever you want...

Demo : http://jsfiddle.net/GpP9e/

l2aelba
  • 21,591
  • 22
  • 102
  • 138
  • Well for me doesn't work.I use jQuery 1.7.2 and i'm trying to incorporate the off with breakpoint.js plugin .When the window is at 780 breakpoint i use .off – Code Jun 17 '13 at 18:05
  • Well, 1.7.2 , use bind and unbind :D @Code – l2aelba Jun 17 '13 at 21:52