get element height excluding descendants
up vote
0
down vote
favorite
Given:
<li>I want this height
<ul>
<li>but not this height</li>
</ul>
</li>
I want to get the height of the first <li>
element, excluding the descendant <ul><li>...
content.
offsetHeight
, clientHeight
, scrollHeight
etc. all give the height of the entire outer <li>...</li>
element.
html height html-lists element
add a comment |
up vote
0
down vote
favorite
Given:
<li>I want this height
<ul>
<li>but not this height</li>
</ul>
</li>
I want to get the height of the first <li>
element, excluding the descendant <ul><li>...
content.
offsetHeight
, clientHeight
, scrollHeight
etc. all give the height of the entire outer <li>...</li>
element.
html height html-lists element
Are you using Vanilla JavaScript or jQuery?
– Samir
yesterday
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Given:
<li>I want this height
<ul>
<li>but not this height</li>
</ul>
</li>
I want to get the height of the first <li>
element, excluding the descendant <ul><li>...
content.
offsetHeight
, clientHeight
, scrollHeight
etc. all give the height of the entire outer <li>...</li>
element.
html height html-lists element
Given:
<li>I want this height
<ul>
<li>but not this height</li>
</ul>
</li>
I want to get the height of the first <li>
element, excluding the descendant <ul><li>...
content.
offsetHeight
, clientHeight
, scrollHeight
etc. all give the height of the entire outer <li>...</li>
element.
html height html-lists element
html height html-lists element
edited 10 hours ago
asked yesterday
nevf
1,27651728
1,27651728
Are you using Vanilla JavaScript or jQuery?
– Samir
yesterday
add a comment |
Are you using Vanilla JavaScript or jQuery?
– Samir
yesterday
Are you using Vanilla JavaScript or jQuery?
– Samir
yesterday
Are you using Vanilla JavaScript or jQuery?
– Samir
yesterday
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
You can extract height of LI
and UL
separately and then subtract it to get the height of LI
only.
Using jQuery
library it can be done as,
var liHeight = $('#test > li').outerHeight(); // Get First Li Height
var liUlHeight = $('#test li > ul').outerHeight(); // Get Height of First UL inside Li
var onlyliHeight = liHeight - liUlHeight;
alert('Full LI Height='+liHeight+' -- UL Height='+liUlHeight+' -- Only LI Height='+onlyliHeight);
Working Demo
Using Vanilla JavaScript,
var liHeight = document.querySelector('#test > li').offsetHeight; // Get First Li Height
var liUlHeight = document.querySelector('#test li > ul').offsetHeight; //Get Height of First UL inside Li
var onlyliHeight = liHeight - liUlHeight;
alert('Full LI Height='+liHeight+' -- UL Height='+liUlHeight+' -- Only LI Height='+onlyliHeight);
Working Demo
Output
Full LI Height=42 -- UL Height=22 -- Only LI Height=20
Thanks, that would work. That said there could be many nested UL, LI items which would make it a bit more complex and possibly not as performant as one would like. Another option might be to hide the<ul>
so it had no height, but I haven't tried that.
– nevf
10 hours ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You can extract height of LI
and UL
separately and then subtract it to get the height of LI
only.
Using jQuery
library it can be done as,
var liHeight = $('#test > li').outerHeight(); // Get First Li Height
var liUlHeight = $('#test li > ul').outerHeight(); // Get Height of First UL inside Li
var onlyliHeight = liHeight - liUlHeight;
alert('Full LI Height='+liHeight+' -- UL Height='+liUlHeight+' -- Only LI Height='+onlyliHeight);
Working Demo
Using Vanilla JavaScript,
var liHeight = document.querySelector('#test > li').offsetHeight; // Get First Li Height
var liUlHeight = document.querySelector('#test li > ul').offsetHeight; //Get Height of First UL inside Li
var onlyliHeight = liHeight - liUlHeight;
alert('Full LI Height='+liHeight+' -- UL Height='+liUlHeight+' -- Only LI Height='+onlyliHeight);
Working Demo
Output
Full LI Height=42 -- UL Height=22 -- Only LI Height=20
Thanks, that would work. That said there could be many nested UL, LI items which would make it a bit more complex and possibly not as performant as one would like. Another option might be to hide the<ul>
so it had no height, but I haven't tried that.
– nevf
10 hours ago
add a comment |
up vote
1
down vote
You can extract height of LI
and UL
separately and then subtract it to get the height of LI
only.
Using jQuery
library it can be done as,
var liHeight = $('#test > li').outerHeight(); // Get First Li Height
var liUlHeight = $('#test li > ul').outerHeight(); // Get Height of First UL inside Li
var onlyliHeight = liHeight - liUlHeight;
alert('Full LI Height='+liHeight+' -- UL Height='+liUlHeight+' -- Only LI Height='+onlyliHeight);
Working Demo
Using Vanilla JavaScript,
var liHeight = document.querySelector('#test > li').offsetHeight; // Get First Li Height
var liUlHeight = document.querySelector('#test li > ul').offsetHeight; //Get Height of First UL inside Li
var onlyliHeight = liHeight - liUlHeight;
alert('Full LI Height='+liHeight+' -- UL Height='+liUlHeight+' -- Only LI Height='+onlyliHeight);
Working Demo
Output
Full LI Height=42 -- UL Height=22 -- Only LI Height=20
Thanks, that would work. That said there could be many nested UL, LI items which would make it a bit more complex and possibly not as performant as one would like. Another option might be to hide the<ul>
so it had no height, but I haven't tried that.
– nevf
10 hours ago
add a comment |
up vote
1
down vote
up vote
1
down vote
You can extract height of LI
and UL
separately and then subtract it to get the height of LI
only.
Using jQuery
library it can be done as,
var liHeight = $('#test > li').outerHeight(); // Get First Li Height
var liUlHeight = $('#test li > ul').outerHeight(); // Get Height of First UL inside Li
var onlyliHeight = liHeight - liUlHeight;
alert('Full LI Height='+liHeight+' -- UL Height='+liUlHeight+' -- Only LI Height='+onlyliHeight);
Working Demo
Using Vanilla JavaScript,
var liHeight = document.querySelector('#test > li').offsetHeight; // Get First Li Height
var liUlHeight = document.querySelector('#test li > ul').offsetHeight; //Get Height of First UL inside Li
var onlyliHeight = liHeight - liUlHeight;
alert('Full LI Height='+liHeight+' -- UL Height='+liUlHeight+' -- Only LI Height='+onlyliHeight);
Working Demo
Output
Full LI Height=42 -- UL Height=22 -- Only LI Height=20
You can extract height of LI
and UL
separately and then subtract it to get the height of LI
only.
Using jQuery
library it can be done as,
var liHeight = $('#test > li').outerHeight(); // Get First Li Height
var liUlHeight = $('#test li > ul').outerHeight(); // Get Height of First UL inside Li
var onlyliHeight = liHeight - liUlHeight;
alert('Full LI Height='+liHeight+' -- UL Height='+liUlHeight+' -- Only LI Height='+onlyliHeight);
Working Demo
Using Vanilla JavaScript,
var liHeight = document.querySelector('#test > li').offsetHeight; // Get First Li Height
var liUlHeight = document.querySelector('#test li > ul').offsetHeight; //Get Height of First UL inside Li
var onlyliHeight = liHeight - liUlHeight;
alert('Full LI Height='+liHeight+' -- UL Height='+liUlHeight+' -- Only LI Height='+onlyliHeight);
Working Demo
Output
Full LI Height=42 -- UL Height=22 -- Only LI Height=20
answered yesterday
Samir
4,8882626
4,8882626
Thanks, that would work. That said there could be many nested UL, LI items which would make it a bit more complex and possibly not as performant as one would like. Another option might be to hide the<ul>
so it had no height, but I haven't tried that.
– nevf
10 hours ago
add a comment |
Thanks, that would work. That said there could be many nested UL, LI items which would make it a bit more complex and possibly not as performant as one would like. Another option might be to hide the<ul>
so it had no height, but I haven't tried that.
– nevf
10 hours ago
Thanks, that would work. That said there could be many nested UL, LI items which would make it a bit more complex and possibly not as performant as one would like. Another option might be to hide the
<ul>
so it had no height, but I haven't tried that.– nevf
10 hours ago
Thanks, that would work. That said there could be many nested UL, LI items which would make it a bit more complex and possibly not as performant as one would like. Another option might be to hide the
<ul>
so it had no height, but I haven't tried that.– nevf
10 hours ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349240%2fget-li-element-height-excluding-descendants%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you using Vanilla JavaScript or jQuery?
– Samir
yesterday