473,819 Members | 3,109 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Obtain height of a SELECT element.

Is there a way to obtain the height of a <SELECT> element dynamically,
i.e. through javascript?

I want to dynamically display a list box onFocus of a text box
element. Also, if the list box would move out of the bottom screen
area, I would want to move it up by that fraction so that it displays
at the bottom of the screen. To do this, I would need to acquire the
height of the SELECT element. I tried quite a few methods.

elem.offsetHeig ht // gives 0.
elem.style.heig ht // gives nothing.
elem.style.pixe lHeight // gives 0.
elem.style.posH eight // gives 0.

I also tried elem.clientHeig ht, which provides a value, but only after
the list is displayed atleast once ( the text box is focussed at least
once )

Our working environment is restricted to IE 5+, and a solution that is
constrained to that will also do. ( i.e. ) if there is any.

Any help/advice would be greatly appreciated.

Regards,
Rithish.
Jul 23 '05 #1
4 5922
Rithish wrote:
Is there a way to obtain the height of a <SELECT> element dynamically,
i.e. through javascript?
Generelly, yes. But whether and how depends on the DOM of the UA.
I want to dynamically display a list box onFocus of a text box
element. Also, if the list box would move out of the bottom screen
area, I would want to move it up by that fraction so that it displays
at the bottom of the screen. To do this, I would need to acquire the
height of the SELECT element. I tried quite a few methods.

elem.offsetHeig ht // gives 0.
elem.style.heig ht // gives nothing.
elem.style.pixe lHeight // gives 0.
elem.style.posH eight // gives 0.


Well, offsetHeight should have worked. The question is rather how do
you get `elem' and how the "select" element's source code looks like.
PointedEars
Jul 23 '05 #2
Rithish wrote:
Is there a way to obtain the height of a <SELECT> element dynamically,
i.e. through javascript?

I want to dynamically display a list box onFocus of a text box
element. Also, if the list box would move out of the bottom screen
area, I would want to move it up by that fraction so that it displays
at the bottom of the screen. To do this, I would need to acquire the
height of the SELECT element. I tried quite a few methods.

elem.offsetHeig ht // gives 0.
elem.style.heig ht // gives nothing.
elem.style.pixe lHeight // gives 0.
elem.style.posH eight // gives 0.

I also tried elem.clientHeig ht, which provides a value, but only after
the list is displayed atleast once ( the text box is focussed at least
once )

Our working environment is restricted to IE 5+, and a solution that is
constrained to that will also do. ( i.e. ) if there is any.

Any help/advice would be greatly appreciated.

Regards,
Rithish.


Actually, to make the <select> scroll into the viewport in IE6SP1 and
Gecko-based browsers use:

<a href="#"
onclick="docume nt.getElementBy Id('mySelectId' ).scrollIntoVie w(false);return
false;">Scroll</a>
<p>Text</p><p>Text</p><p>Text</p><p>Text</p>
<p>Text</p><p>Text</p><p>Text</p><p>Text</p>
<p>Text</p><p>Text</p><p>Text</p><p>Text</p>
<p>Text</p><p>Text</p><p>Text</p><p>Text</p>
<p>Text</p><p>Text</p><p>Text</p><p>Text</p>
<p>Text</p><p>Text</p><p>Text</p><p>Text</p>
<form>
<select name="mySelectN ame" id="mySelectId " size="4">
<option>text</option>
<option>text</option>
<option>text</option>
<option>text</option>
<option>text</option>
<option>text</option>
<option>text</option>
</select>
</form>

scrollIntoView( ) takes a boolean parameter, true if you want the element
to be scrolled to the top of the view port, false if you want it to scroll
only far enough to be completely visible.

If you want it to work in IE6SP1, Gecko-based browsers and Opera 7.53, you
can use:

<a href="#"
onclick="
window.scrollTo (
0,
document.getEle mentById('mySel ectId').offsetT op +
document.getEle mentById('mySel ectId').offsetH eight
);
return false;
">Scroll</a>

(split across multiple lines to avoid wrapping issues)

Note, with either of these solutions, you should wrap it in a function and
test for the appropriate methods and properties before attempting to use
them.

--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq
Jul 23 '05 #3
> Well, offsetHeight should have worked. The question is rather how do
you get `elem' and how the "select" element's source code looks like.
PointedEars


The <SELECT> element is defined within a <DIV> and the <DIV> is hidden
by default. Onfocus of the textBox, the <DIV> would be displayed
again.

Well.. I did a bit of trial and error and found out this piece of
information.

I was using the display property of the STYLE attribute to
display/hide the DIV. ( <DIV STYLE='display: none'> ). I found that the
first time the txtBox is set focus, offsetHeight of the <SELECT>
element returns 0. However, on subsequent focus, offsetHeight returns
a value. Why and why not? I am stumped.
I substituted the 'display' property with 'visibility' ( <DIV
STYLE='visibili ty:hidden'> ) and lo.. all is fine. offsetHeight always
returns a value.
Regards,
Rithish.
Jul 23 '05 #4
Rithish wrote:
Well, offsetHeight should have worked. The question is rather how do
you get `elem' and how the "select" element's source code looks like.
[...]


The <SELECT> element is defined within a <DIV> and the <DIV> is hidden
[display:none] by default. Onfocus of the textBox, the <DIV> would be
displayed again.
[...]
I substituted the 'display' property with 'visibility' ( <DIV
STYLE='visibili ty:hidden'> ) and lo.. all is fine. offsetHeight always
returns a value.


Of course. In contrast to visibility:hidd en, display:none prevents the UA
from rendering the element. Where nothing is rendered, no offset-height
can be computed.
PointedEars
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
21549
by: delerious | last post by:
I need to determine an element's width and height in pixels (not including padding, border, and margin) in Javascript. The element will not have width or height styles specified. In Mozilla, I know I can use document.defaultView.getComputedStyle() to do this. IE does not support document.defaultView.getComputedStyle(). It supports offsetWidth/offsetHeight, but those include the padding and border. IE also supports...
12
6745
by: Stanimir Stamenkov | last post by:
Here are two cases regarding inline-level elements' line-height, padding and background, which I doesn't understand: <div style="background: black; color: white; line-height: 1.5"> <span>Abc</span> <span style="background: white; color: black; line-height: 3">Abc</span> <span>Abc</span> </div>
5
2915
by: Secret Guy | last post by:
Because of my experience posting various places over the last couple of weeks: I'm expecting to be greated with hostility for asking about concepts instead of "practical" things, since that has been my experience for years and decades. Maybe I'm projecting hostility. I don't know. How can I ask about theory and concepts without offending people? I'll just continue asking and cross my fingers: According to the 2.1 spec, regarding...
3
2471
by: gooderthanyou | last post by:
I have a textfield and you of course you can select text... When they hit the bold button I want it to obtain the selected text and bold it, the hard part is trying to figure out if javascript can even OBTAIN selected text?!?! I can do this in java if I have to, I know java better than javascript, it seems as that is the only way to do it from looking online. <-- me javascript beginner
3
6787
by: Jarek Mielcarek | last post by:
hi all, in xml file I have some fields which are source for <textarea> element. I'd like to transform this file using xslt and set the rows property of <textarea> depend of lines in some source field. How to do this? regards jaro
5
18718
by: Kevin Myers | last post by:
How can one obtain obtain the height of a MS Access 2K form? There is a width property, but I don't see a corresponding height property. I need to obtain the height of the form so that I can dynamically resize an object on the form in the form's Form_Resize method when a user resizes the form, both horizontally and vertically. Thanks, s/KAM
6
2717
by: Martin Plotz | last post by:
Dear Newsgroup, I would like to have a page with a footer on the bottom of the page with a fixed height. The upper part should have the remaining height. In frames I could achieve this with "*" as height for the upper part - but I would like to do the same with CSS. In Firefox <body style="height:100%"> <div id="main"
3
22295
by: =?iso-8859-1?q?Jesper_R=F8nn-Jensen?= | last post by:
I'm working with a requirement to make a selectbox the same height as normal text input elements. (at least for IE7) But in IE the selectbox reacts strangely to the CSS rules. Normally, there are so many things not possible with select boxes in IE. But the I discovered I could actually change the height by adjusting font-size, then adjusting height I was actually able to tweak the height.
3
3326
by: html | last post by:
I have a select tag as follows <SELECT ID="Select1" NAME="Select1" ONCHANGE="do_alert()"> <OPTION>1</OPTION> <OPTION>2</OPTION> </SELECT> I need, when the user choose an option, to print an alert which displays the text in the option chosen, i.e. 1 or 2 in this example, but how do I access the value of the html
0
9748
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10703
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10427
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10457
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10159
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9248
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7700
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3917
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3046
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.