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

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 08-17-2012, 09:25 PM   PM User | #1
resin
Regular Coder

 
Join Date: Nov 2011
Posts: 142
Thanks: 5
Thanked 0 Times in 0 Posts
resin is an unknown quantity at this point
detecting display of element?

How can I allow a jQuery function to execute only if a certain div is displayed (or hidden)?
resin is offline   Reply With Quote
Old 08-17-2012, 09:50 PM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Code:
if ($('#somediv').css('display') != 'none' && $('#somediv').css('visibility') != 'hidden') {
    // function or call
}
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 08-18-2012, 12:30 AM   PM User | #3
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,706
Thanks: 5
Thanked 876 Times in 851 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
It’s even easier than this, depending on what you’re trying to do:
PHP Code:
$('#example:visible').blah… 
or
PHP Code:
if($('#example:visible').length) {
  
// stuff…

or
PHP Code:
if($('#example').is(':visible')) {
  
// stuff…

__________________
Don’t click this link!
VIPStephan is online now   Reply With Quote
Old 08-18-2012, 12:57 AM   PM User | #4
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
:visible is actually looking for display:none (and a couple of other circumstances) but (I understand) considers visibility: hidden as still being visible - since it still occupies space in the layout?!

I suppose most of the time this is not a concern as people tend to be confirming the application of show() or hide().
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 08-18-2012, 10:24 AM   PM User | #5
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,706
Thanks: 5
Thanked 876 Times in 851 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Oh, you’re right. But let’s let the OP decide what works best for them.
__________________
Don’t click this link!
VIPStephan is online now   Reply With Quote
Old 08-18-2012, 01:45 PM   PM User | #6
resin
Regular Coder

 
Join Date: Nov 2011
Posts: 142
Thanks: 5
Thanked 0 Times in 0 Posts
resin is an unknown quantity at this point
The :visible method will work fine in this case, but Andrew's method is not working for me, seems the if statement is being completely ignored and the function executes as if it weren't there. I was looking forward to using that method for other css properties as well, might you know why it isn't working?
resin is offline   Reply With Quote
Old 08-18-2012, 02:06 PM   PM User | #7
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
The if statement won't be ignored, it's just not meeting the conditions.

Try using alerts (or console.log) to check what these values are:

Code:
window.alert($('#somediv').css('display'));
window.alert($('#somediv').css('visibility'));
.. and, of course, you've modified '#somediv' to the correct id for your own div element?
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 08-18-2012 at 02:09 PM..
AndrewGSW is offline   Reply With Quote
Old 08-19-2012, 07:54 PM   PM User | #8
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,706
Thanks: 5
Thanked 876 Times in 851 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
If you do what Andrew suggested in the previous post you’ll see that the alert is giving the value of the attribute. That way you can check what comes out and whether it matches the condition.
__________________
Don’t click this link!
VIPStephan is online now   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 11:58 AM.


Advertisement
Log in to turn off these ads.