I'm trying to add a $('body').live('click')
listener after the user clicks on a div. The purpose is creating a custom dropdown box that the user can open and eventually close by clicking anywhere on the page.
But when I add .live()
or .bind()
function inside a .click()
function, the live()
function is triggered for some reason:
$('#myDiv').click(function(){
$('#myDiv .child').show().addClass('showing');
$('body').live('click', function(){
$('#myDiv .child').hide();
$('body').die();
})
})
#myDiv
is shown but immediately hides, evidenced by the lingering .showing
class.
Am I using live()
wrong? How do I avoid this recursion?