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.

Pierre
  • 12,468
  • 6
  • 44
  • 63

4 Answers4

2
if($("#myTab").is(':visible')){
    here is your code ...
}
jumancy
  • 1,290
  • 1
  • 9
  • 16
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
}
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
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.

Community
  • 1
  • 1
Alnitak
  • 334,560
  • 70
  • 407
  • 495
-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
}
rlemon
  • 17,518
  • 14
  • 92
  • 123
Kunal Vashist
  • 2,380
  • 6
  • 30
  • 59
  • 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 – Elias Van Ootegem Apr 30 '12 at 10:52
  • the comment above mine is correct. the code you provided throws a syntax error. – rlemon Jul 16 '14 at 12:22