14

I have a div and I'd like to have an event handler listen to when it becomes visible and hidden. How do you do that?

Thanks.

frenchie
  • 51,731
  • 109
  • 304
  • 510
  • 2
    i think your question was answered already here: http://stackoverflow.com/questions/1225102/jquery-event-to-trigger-action-when-a-div-is-made-visible – Benjamin Seiller Feb 09 '11 at 17:42
  • There is a similar question [here](http://stackoverflow.com/questions/1225102/jquery-event-to-trigger-action-when-a-div-is-made-visible) See my answer http://stackoverflow.com/a/21242078/848034 – tlogbon Jan 20 '14 at 19:13

2 Answers2

15

You can use the callback parameter in the show() and hide() methods like this:

$('#myDiv').show(0, onDivShow);
$('#myDiv').hide(0, onDivHide);

function onDivShow() { //your code here }
function onDivHide() { //your code here }

See a working example here: http://jsfiddle.net/N7UNU/

treeface
  • 13,270
  • 4
  • 51
  • 57
  • the showing of the div is controlled by an asp updateprogress control: I can't write $('#myDiv').show(0, onDivShow). I need to listen to the "div just turned visible" event. – frenchie Feb 09 '11 at 17:36
  • @frenchie: there is no native event to do this in JavaScript. The only real options you have are to use a custom event that you trigger when you show or hide the items or to skip that step and do what I've done above. – treeface Feb 09 '11 at 17:40
1

You can create a trigger. You would, of course, have to fire the trigger, but that is one way to do it.

Jason
  • 51,583
  • 38
  • 133
  • 185