Alexander Abramovich
Alexander Abramovich

Reputation: 11458

Finding all the visible elements using the given root

Given the following code

var a = $('<div><div></div></div>');
a.css("visibility", "visible");
a.find("* :visible");

I receive an empty array [] as a result instead of a div. What am I doing wrong?

Upvotes: 2

Views: 86

Answers (2)

Rory McCrossan
Rory McCrossan

Reputation: 337626

To check if an element is visible, it must be inserted into the DOM. You also don't need the * selector. Try this:

var a = $('<div><div></div></div>'); // create an element
a.css("visibility", "visible");
$("BODY").append(a) // Add the element to the DOM first
a.find(":visible")

alert(a.find(":visible").length); // displays '1'

Upvotes: 3

Guffa
Guffa

Reputation: 700562

You haven't added the element to the page, so it's not sized yet. Elements with zero size are not considered visible.

Upvotes: 1

Related Questions