Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-17-2013, 11:37 PM   PM User | #1
snakehill
New Coder

 
Join Date: Sep 2011
Posts: 19
Thanks: 4
Thanked 0 Times in 0 Posts
snakehill is an unknown quantity at this point
Alert visible elements inside div (native javascript)

I was wondering if there is any way to alert or get to only the visible elements within a div, span or any parent element.

When using innerHTML, it obviously shows everything, including hidden elements that are only to be found in the source code. I need only the visible elements.

If it would help anything, all the invisible elements are hidden via display:none CSS (either set beforehand or dynamically via Javascript).

Thanks!
snakehill is offline   Reply With Quote
Old 02-17-2013, 11:39 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Are they hidden with inline style="visibility: hidden;" or style="display: none;"??

Or are they hidden via a class name and the <style> block?

Yes, it makes a difference.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-17-2013, 11:41 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
And do you care that alert( ) is considered obsolete coding?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-17-2013, 11:59 PM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
you can loop through all of the elements checking for the
Code:
getPropertyValue("visibility")
of their getComputed Style (and whatever the IE equivalent is) - this will exclude not only those that are hidden, but also those that are nested within elements that are hidden
xelawho is offline   Reply With Quote
Old 02-18-2013, 12:06 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
... of their getComputedStyle (and whatever the IE equivalent is) ...
Which is why I asked about <style> vs style="...".

If they are all declared using style="...", then you don't have to use getComputeStyle and its IE equivalent.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-18-2013, 12:08 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
But the other point:

If you don't use the obsolete alert( ) to do this and instead use a popon <div>, you can just copy the innerHTML and in the popon the visibilities will *likely* be the same so you don't have to change anything. ("likely" because it's possible the style depends on the container something is in.)
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-18-2013, 12:09 AM   PM User | #7
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
and if they are nested inside a hidden div but are inline styled visible?
xelawho is offline   Reply With Quote
Old 02-18-2013, 12:14 AM   PM User | #8
snakehill
New Coder

 
Join Date: Sep 2011
Posts: 19
Thanks: 4
Thanked 0 Times in 0 Posts
snakehill is an unknown quantity at this point
I am pretty sure I stated it is via "display: none;" (also when it's about any dynamic changes).
It is actually a combination of dynamic changes and <style> block.

And I do consider alert() to be obsolete, but I use it all the time to test Javascript functions to see if they works and output anything correctly.

It seems a bit weird to me that you would have to loop all of the different child elements. I just would like to get to what is in the element, just what is to be seen.
snakehill is offline   Reply With Quote
Old 02-18-2013, 12:25 AM   PM User | #9
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
you could try it without a loop but to me it seems unlikely that you will save any time or code doing it that way. Maybe with a query selector, but even then you would have to loop through the selected elements to alert() them.

Much depends, I guess, on the structure of your html
xelawho is offline   Reply With Quote
Old 02-18-2013, 12:25 AM   PM User | #10
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Nope. Given all that, Xelawho is right.

If you use getComputedStyle(), you could do something like this:
Code:
var container = document.getElementById("yourContainerDiv");
var result = "";
var elems = container.getElementsByTagName("*"); // all elements, nested or not
for ( var e = 0; e < elems.length; ++e )
{
    var elem = elems[e];
    var elemStyle = getComputedStyle( elem ); // but need to provide IE alternative
    if ( elemStyle.display != "none" && elemStyle.visibility != "hidden" )
    {
        result += "\n" + elem.innerText;
    }
}
Or something along those lines. Not sure if you wanted to see all the tag names in the alert() or not.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-18-2013, 12:31 AM   PM User | #11
snakehill
New Coder

 
Join Date: Sep 2011
Posts: 19
Thanks: 4
Thanked 0 Times in 0 Posts
snakehill is an unknown quantity at this point
That seems to be it. I'll look into getComputedStyle() and any IE alternative. Thanks for any help, especially the quickness of it!
snakehill is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:51 PM.


Advertisement
Log in to turn off these ads.