0

There are a number of posts on how to get/set the height of hidden elements (e.g. this and this).

What I want, though, is a height query which gives the answer 0 for hidden elements and the visible height for visible elements. I can do it with a bunch of if statements, but is there a simpler way?

Thanks.

Community
  • 1
  • 1
Nick
  • 5,995
  • 12
  • 54
  • 78
  • Do you need the 0 returned or Could you use the :visible selector in jQuery to only look at height of visible elements? – Brett Smith Jun 24 '12 at 02:25
  • I could certainly use the `:visible` selector. However, it's in a onresize function and there's a bunch of checks I need to make, so the less DOM queries, the better :) – Nick Jun 24 '12 at 02:28
  • post some code and html as a refernece. Show what you have tried – charlietfl Jun 24 '12 at 02:35

1 Answers1

6

You could make your own jQuery method that would do what you want:

jQuery.fn.visHeight = function() {
    var h = this.height();
    if (h != 0 && !this.is(":visible")) {
        h = 0;
    }
    return(h);
};
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thanks. I'm in the throes of doing similarly, although I'll enquire if it is `visible` first (given that it will always have height in the situation I'm dealing with). I conclude from your answer that there's no in-built jQuery op that does this for you (which was the essence of my question). – Nick Jun 24 '12 at 02:38
  • You didn't show us the whole problem you're trying to code, but you could just eliminate the hidden items from the jQuery object in the first place with a change to the original selector or a filter so you never even ask hidden items for their height. I'm not aware of any built-in jQuery capability for this. – jfriend00 Jun 24 '12 at 02:42
  • Mate, I'm not quite sure why I needed to show you "the whole problem". I would have there's not a lot of ambiguity in this: "What I want, though, is a height query which gives the answer 0 for hidden elements and the visible height for visible elements." – Nick Jun 24 '12 at 02:50
  • @Nick - I'm just suggesting that there are probably other ways to solve the problem if you move the problem upstream and eliminate the hidden items before you inquire about the height. You don't have to do it that way if you don't want - just another idea. It is very often here on SO that we get a detailed question about one aspect of a solution when there are even better ways to solve the problem if the entire problem is disclosed. No obligation on your part to disclose that, but that's why we ask sometimes. – jfriend00 Jun 24 '12 at 02:51
  • Thanks. Please overlook my general frustration with this site. I am, with the exception of the previous comment, unfailingly polite and as clear as I can be in my questions. I tire, however, of knee-jerk requests to "post some code and html as a refernece. Show what you have tried" (as above), or unthinking, anonymous -1 responses to perfectly legitimate efforts (e.g., http://stackoverflow.com/questions/11061414/resize-elements-by-dragging-divider-handler) – Nick Jun 24 '12 at 02:57
  • @Nick, he asked it only to understand the need of your actual problem and to answer your question in a better way. – The Alpha Jun 24 '12 at 02:57
  • @jfriend00 I accept that this is probably "my bad" in common parlance. Nevertheless, I find it a little galling that a straight forward, "narrow" question is frequently interpreted as an invitation to critique and hypothesise about all the assumptions that may underlie the question. I also confess that your own non-response to some of my earlier light-hearted comments/banter has not engendered the most positive attitude :) Nevertheless, I appreciate and commend you for your service in the SO community. – Nick Jun 24 '12 at 03:16
  • @Nick - many questions here on SO are too narrow. They ask for one little tiny piece of a solution when they are actually down a less efficient or wrong path and if they described the overall problem they are trying to solve, there are much, much better ways to approach the issue. When I teach, I figure I'm offering the most benefit if I can understand what the real issue is and perhaps offer an even better path than the exact one they are asking about. Sorry if that bothers you. I'm not sure what you thought my "non-response" was. I will be offline for a couple days. – jfriend00 Jun 24 '12 at 06:00
  • Thanks, @jfriend00 Thanks for taking the time to respond. It's probably not worth going through specifics. I appreciate your desire to treat situations holistically. From my time-poor perspective, though, it's often neither possible or worthwhile to detail the precise scenario, given that the page I'm working on has thousands of elements and nesting 15 layers deep at some points. So someone asks for more details ('post your code') in a effort that takes him/her 40 seconds, and then will -1 if I politely demur. Life without 56K of rep is a different game (as my prev link shows) :) – Nick Jun 24 '12 at 08:14