treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Remove event when clicked?

  • Hi,
    I've got an element with a function attached like this:

    $('div').someEvent();

    When I click the div, I want the function to go away until I click the div again. How would I toggle the event?
  • You need to .unbind() it to remove it, and call it again to add it:

    http://docs.jquery.com/Events/unbind

    You can unbind specific events, or call it with no params to unbind all of them.
  • Not sure what event you're using, but it's worth noticing the hover event is kind of special when using unbind()... In that very occasion you can't just use $('#selector').unbind('hover'), but you'll have to unbind both the mouseover and the mouseout event...