2

I'm trying to execute jQuery code when a div's style changes from display: none; to display: block;

I'm using tabs that's why this div's style changes like this, and this jQuery code should only be running when viewing this tab only.

so how can I achieve this ?

and thanks.

EDIT: I tried .is(':visible'); and it only works if we load the page and this div is visible, not if this div will be visible later.

4
  • 2
    Why not execute the code when a user clicks on the other tab? Commented Apr 30, 2012 at 10:43
  • yes maybe this will work, but I want to know if it's possible to do this using jQuery, listening when something happened ? Commented Apr 30, 2012 at 10:54
  • @Alnitak ok so I guess I have to do it the way Celos suggested above. Commented Apr 30, 2012 at 11:00
  • @Peter yeah, it's normally quite easy to trap any event which changes the displayed tab. Commented Apr 30, 2012 at 11:03

4 Answers 4

2

What you're looking for are DOM Mutation Events, which can raise an event when a DOM element is inserted, deleted or modified.

Unfortunately they were never well supported, and were deprecated in DOM3.

In any event (no pun intended), in your case it would be simpler just to catch whatever UI event it is that causes the tab to be displayed.

Sign up to request clarification or add additional context in comments.

Comments

2
if($("#myTab").is(':visible')){
    here is your code ...
}

3 Comments

missing colon from your visible psuedo selector
No worries, thought it was just a typo. :)
OP appears to want an event raised, not to test the visibility
2

try something like this:

if($(DIV).is(':visible')) {   // DIV => valid selector of your target
   // execute your code
}

OR:

if($('body').on('EVENTNAME', 'DIV:visible')) {   // DIV => valid selector of your target
   // execute your code
}

1 Comment

OP appears to want an event raised, not to test the visibility
-1
$(document).ready(function(){
    $(your tab container).click(function(){
        //set none property to all of your id's
        var obj=$this;
        if(obj){
            $("your id").style.display="block"; // set block to your current item
        }
    });
});

or

if($("you id").is('visible')){
    //do it
}

2 Comments

Does a jQuery object even have access to the style property? I thought it din't. So $("your id").style.display="block"; won't work. you either have to change it to $("your id")[0].style.display="block"; or $("your id").get(0).style.display="block"; or use the jQuery object itself: $("your id").attr('style','display:block'); or something similar
the comment above mine is correct. the code you provided throws a syntax error.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.