PDA

View Full Version : Select box width


shoofy
10-05-2004, 06:53 PM
Is there a way to get the width of a select element in pixels using javascript? I tried document.getElementById("select1").width anddocument.getElementById("select1").style.width and niether worked. The Mozilla javascript console says that doucment.getElementById("select1") has no properties. How can I do this?

allanm
10-06-2004, 12:33 AM
The second option should work in IE6 at least.

But you must ensure that the element actually does have a width property. For example:

This first element example doesn't have a width value so the alert will give you nothing:



<select id="dropDown">
<option></option>
<option></option>
</select>

alert(document.getElementById("dropDown").style.width);



Whereas an alert on this element will display 100px because it does have a width property.



<select id="dropDown" style="width:100px">
<option></option>
<option></option>
</select>

alert(document.getElementById("dropDown").style.width);


Not sure if that's what you mean, but it may help you.

Cheers.

shoofy
10-06-2004, 03:56 PM
I wrote an alternative script to serve my purpose. I put a span tag that contains just a blank space (because IE ignored the span when it was empty) and labeled it 'theend.' To get the width of the select box, I used the following script:var selw = (document.getElementById("theend").offsetLeft - document.getElementById("select").offsetLeft)