Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's the best way to test an element in the DOM
    primarykey
    data
    text
    <p>There are a couple of ways I could do this (That I'm aware of).</p> <p>Test css display</p> <pre><code>if ($('#foo').css('display') == 'none') </code></pre> <p>Test the visibility</p> <pre><code>if ($('#foo').is(':visible')) </code></pre> <p>In the visibility I can check if the element is there.</p> <blockquote> <p>Elements are considered visible if they consume space in the document. Visible elements have a width or height that is greater than zero.</p> <p>Elements with visibility: hidden or opacity: 0 are considered visible, since they still consume space in the layout.</p> </blockquote> <p><a href="http://api.jquery.com/visible-selector/" rel="nofollow noreferrer">Source</a></p> <p>But, note that in both I can't test the visibility (by the user) because:</p> <blockquote> <p>Hiding an element can be done by setting the display property to "none" or the visibility property to "hidden". However, notice that these two methods produce different results:</p> <p><strong>visibility:</strong> hidden hides an element, but it will still take up the same space as before. The element will be hidden, but still affect the layout.</p> <p><strong>display:</strong> none hides an element, and it will not take up any space. The element will be hidden, and the page will be displayed as if the element is not there:</p> </blockquote> <p><a href="http://www.w3schools.com/css/css_display_visibility.asp" rel="nofollow noreferrer">Source</a></p> <p>So in neither of the examples I test if the element is visible in all senses for the user.</p> <p>So my question is:</p> <ul> <li><strong>What're the differences between the two if's codes from above?</strong></li> <li><strong>What's the best way to test if an element is visible to the user:</strong></li> </ul> <p>Should I have to use something like:</p> <pre><code>if ($('#foo').is(':visible') &amp;&amp; $('#foo').css('opacity') &gt; 0 &amp;&amp; $('#foo').css('visibility') != 'hidden') </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload