Mark
Mark

Reputation: 2587

jQuery on DOM element found at any time

In jQuery I want to say at any time this DOM element shows up do this. So kinda like .live() or .on() at any time the DOM element is found, hide it.

Upvotes: 2

Views: 101

Answers (2)

j08691
j08691

Reputation: 207861

You don't need jQuery, just use CSS. Even if the element is generated after the page is created the CSS will be applied. Ex: http://jsfiddle.net/YB2bk/. In this example, a page has one div that when clicked, creates a new div. Both divs get the same CSS applied.

Upvotes: 0

Fenton
Fenton

Reputation: 250812

Say you wanted to achieve this...

if (myCondition) {
    $(".myClass").hide();
}

But you don't know when the myClass item will show up, you could do the following:

CSS:

.shouldHide .myClass {
    display: none;
}

jQuery:

if (myCondition) {
    $("body").addClass("shouldHide");
}

This means you can still apply the condition by adding / removing the shouldHide class on the body tag, and you have a CSS rule that will hide myClass if the body tag has the shouldHide class.

Upvotes: 2

Related Questions