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.

2

2 Answers 2

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/

2
  • 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 9, 2011 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 9, 2011 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.