L4zl0w
L4zl0w

Reputation: 1097

JQuery: event listener removed after display:none?

I have a button with a "click" event listener defined on it with jQuery bind(). The event listener works fine until I (programmatically) do a display:none on the parent element (effectively hiding the parent element and the button). When I un-hide the parent element (display:block) the button doesn't work any more.

Example: let's say I have the following HTML markup with the following event listener:

$( ".my-button" ).bind( "click", myFunction );

<div class="container">
  <div class="my-button">Button</div>
</div>

If I, with another jQuery function, do a

$(".container").css('display', 'none');

the event listener disappears. Is this behaviour expected? Do I need to re-bind the event listener after each display:none?

Upvotes: 3

Views: 2333

Answers (1)

loveNZ
loveNZ

Reputation: 307

http://jsfiddle.net/gadL2rag/

$(".container").css('display', 'none'); 

will not unbind the event, it will hide the .container div and its children. but after you call $(".container").css('display', 'block'); again, the event is still there, you don't have to bind it again. hope this help

Upvotes: 1

Related Questions