jQuery unbind click event function and re-attach depending on situation

binding, click, events, javascript, jquery

Solution

Simply make a named function. You can go low tech here and back to basics to unbind and reattach specific events.

function doStuff()
{
    if($(this).,next('.section').is(':visible'))
        ...
}

$('.header').on('click', doStuff);
$('.header').off('click', doStuff);

Problem

The code below I use to create a sliding menu. I need to know how to unbind the function attached to the click event and re-attach it some other time. (using jQuery 1.7.2) ``` $(document).ready(function(){ $('.section').hide(); $('.header').click(function(){ if($(this).next('.section').is(':visible')) { $('.section:visible').slideUp() $('.arrows:visible').attr("src","right.gif") } else { $('.section').slideUp(); $(this).next('.section').slideToggle(); $(this).find('.arrows').attr("src","down.gif") }); }); ``` The code below is what I have so far ``` $('#printVers').click(function(){ if($('#formVersion').val() != "Print") { $('.header').unbind('click'); } else { //else re-attach functionality? } }); ``` Thanks

Original source

Related problems