473,387 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

setting style of dynamic options

I'm dynamically adding options to a select list in javascript and i
need to be able to set the height of the option, but setting
style.height has not effect, I also tried style.pixelHeight but no
joy.

i'm doing something like this (:

var selectControl = document.getElementById('MySelect');
var el = document.createElement('option');
el.value = "some value";
el.style.height = "500";
selectControl.appendChild(el);
any ideas?
Jul 23 '05 #1
9 2523
ne********@hotmail.com wrote:
I'm dynamically adding options to a select list in javascript and i
need to be able to set the height of the option, but setting
style.height has not effect, I also tried style.pixelHeight but no
joy.

i'm doing something like this (:

var selectControl = document.getElementById('MySelect');
var el = document.createElement('option');
el.value = "some value";
el.style.height = "500";
selectControl.appendChild(el);
any ideas?


500 what? Pixels?
And why no "el.text"?
Mick
Jul 23 '05 #2
ne********@hotmail.com wrote in message news:<5b**************************@posting.google. com>...
I'm dynamically adding options to a select list in javascript and i
need to be able to set the height of the option, but setting
style.height has not effect, I also tried style.pixelHeight but no
joy.

i'm doing something like this (:

var selectControl = document.getElementById('MySelect');
var el = document.createElement('option');
el.value = "some value";
el.style.height = "500";
selectControl.appendChild(el);
any ideas?


el.style.height = "500px"; //tall option!
Jul 23 '05 #3
ne********@hotmail.com wrote:
I'm dynamically adding options to a select list in javascript and i
need to be able to set the height of the option, but setting
style.height has not effect, I also tried style.pixelHeight but no
joy.

i'm doing something like this (:

var selectControl = document.getElementById('MySelect');
var el = document.createElement('option');
el.value = "some value";
el.style.height = "500";
selectControl.appendChild(el);
any ideas?


You control the style of an option from the select element it's nested
inside. If you are trying to control the size of the option font, use
a style on the select:

<form...>
<select style="font-size: 12pt;">
<option ...>...</option>
...

Naturally you can also use a CSS class rather than an in-line style.

If you are trying to control the height of the box that the options are
displayed inside, use the "size" attribute of the select element:

<form...>
<select style="font-size: 12pt;" size="4">
<option ...>...</option>
...

"size" refers to the number of options displayed, so you may not want
to use 500.

More information can be found here:

<URL: http://www.w3.org/TR/html4/interact/forms.html#h-17.6>

Cheers, Rob.
Jul 23 '05 #4
RobG <rg***@iinet.net.auau> wrote in message news:<PN*****************@news.optus.net.au>...
ne********@hotmail.com wrote:
I'm dynamically adding options to a select list in javascript and i
need to be able to set the height of the option, but setting
style.height has not effect, I also tried style.pixelHeight but no
joy.

i'm doing something like this (:

var selectControl = document.getElementById('MySelect');
var el = document.createElement('option');
el.value = "some value";
el.style.height = "500";
selectControl.appendChild(el);
any ideas?
You control the style of an option from the select element it's nested
inside. If you are trying to control the size of the option font, use
a style on the select:

<form...>
<select style="font-size: 12pt;">
<option ...>...</option>
...

Naturally you can also use a CSS class rather than an in-line style.

If you are trying to control the height of the box that the options are
displayed inside, use the "size" attribute of the select element:

<form...>
<select style="font-size: 12pt;" size="4">
<option ...>...</option>
...

"size" refers to the number of options displayed, so you may not want
to use 500.

More information can be found here:

<URL: http://www.w3.org/TR/html4/interact/forms.html#h-17.6>

Cheers, Rob.

RobG wrote
You control the style of an option from the select element it's nested
inside.


Only in Internet Explorer, where 'windowed' elements (like listboxes)
have limited exposure. More advanced CSS browsers allow styling of
Options like any other element, although some layout aspects,
naturally, pertain to the containing list rather than any specific
option.
Jul 23 '05 #5
RobB wrote:
RobG wrote
You control the style of an option from the select element it's nested
inside.

Only in Internet Explorer, where 'windowed' elements (like listboxes)
have limited exposure. More advanced CSS browsers allow styling of
Options like any other element, although some layout aspects,
naturally, pertain to the containing list rather than any specific
option.


Not just Explorer, Safari is the same (but I haven't tested the latest
version). I expect all the Geko-based browsers support styles on
options, perhaps someone can comment on Opera.

Below is some test code, try changing the height of option 11 to 20em -
additional space is added below option 25 - similarly if you make it
500px as the OP attempted.

A bit of testing in Firefox shows that whilst small heights (say
equivalent to 2 to 5 lines) work fine, large heights are not reliable
and results vary if options have different heights specified. Note
also that the option height affects the select height - even though
only 3 rows are supposed to show, the select is greatly expanded - I
guess that the select height is set to size x the largest line height
which can result in an overly extended select.

Given that the OP didn't see any effect of "height" in their browser,
it's likely that s/he isn't using one of the "more advanced CSS
browsers". And given the majority of web surfers use IE, it's best not
to depend on features it doesn't have.

To top it off, the difficulty of controlling the size of the select
element itself if options are given different heights introduces
apparent unreliability in the UI that users may not understand.

But it's an issue worth discussing - thanks for bringing it up. :-)

Rob.

*Play code follows:*

<form action="">
<select name="aSelect"
style="font-size: 10pt; font-family: sans-serif;" size="3">
<option value="opt1">option 1
<option value="opt2">option 2
<option value="opt3">option 3
<option value="opt4">option 4
<option value="opt5">option 5
<option value="opt6">option 6
<option value="opt7">option 7
<option value="opt8"
style="font-family: courier; font-weight: bold;">option 8
<option value="opt9"
style="font-weight: bold; height: 20px;">option 9
<option value="opt10">option 10
<option value="opt11"
style="font-weight: bold; height: 2em;">option 11
<option value="opt12">option 12
<option value="opt13">option 13
<option value="opt14">option 14
<option value="opt15">option 15
<option value="opt16">option 16
<option value="opt17">option 17
<option value="opt18">option 18
<option value="opt19">option 19
<option value="opt20">option 20
<option value="opt21">option 21
<option value="opt22">option 22
<option value="opt23">option 23
<option value="opt24">option 24
<option value="opt25">option 25
</select><br>
<input type="reset"><br><br>
<input type="text" cols="10" name="aNum">
Enter a number from 1 to 25 and that option will be selected<br>
<input type="button" value="click me" onclick="
var x = this.form.aNum.value;
if (x > 0 && x < 26) {
this.form.aSelect.options[x - 1].selected='true'
} else {
alert('From 1 to 25 please, ' + x + ' is out of bounds');
}
">
</form>
Jul 23 '05 #6
Mick White <mw******@rochester.rr.com> wrote in message news:<e%****************@twister.nyroc.rr.com>...
ne********@hotmail.com wrote:
I'm dynamically adding options to a select list in javascript and i
need to be able to set the height of the option, but setting
style.height has not effect, I also tried style.pixelHeight but no
joy.

i'm doing something like this (:

var selectControl = document.getElementById('MySelect');
var el = document.createElement('option');
el.value = "some value";
el.style.height = "500";
selectControl.appendChild(el);
any ideas?


500 what? Pixels?
And why no "el.text"?
Mick


500 anythings, i tried px, pt, % - none of them made any difference.
I was setting el.innerText, missed that out the code snippet. Sorry.
Jul 23 '05 #7
fe******@hotmail.com (RobB) wrote in message news:<ab**************************@posting.google. com>...
ne********@hotmail.com wrote in message news:<5b**************************@posting.google. com>...
I'm dynamically adding options to a select list in javascript and i
need to be able to set the height of the option, but setting
style.height has not effect, I also tried style.pixelHeight but no
joy.

i'm doing something like this (:

var selectControl = document.getElementById('MySelect');
var el = document.createElement('option');
el.value = "some value";
el.style.height = "500";
selectControl.appendChild(el);
any ideas?


el.style.height = "500px"; //tall option!


tried this, but it doesn't seem to make any difference in IE.
Jul 23 '05 #8
RobG <rg***@iinet.net.auau> wrote in message news:<PN*****************@news.optus.net.au>...
ne********@hotmail.com wrote:
I'm dynamically adding options to a select list in javascript and i
need to be able to set the height of the option, but setting
style.height has not effect, I also tried style.pixelHeight but no
joy.

i'm doing something like this (:

var selectControl = document.getElementById('MySelect');
var el = document.createElement('option');
el.value = "some value";
el.style.height = "500";
selectControl.appendChild(el);
any ideas?


You control the style of an option from the select element it's nested
inside. If you are trying to control the size of the option font, use
a style on the select:

<form...>
<select style="font-size: 12pt;">
<option ...>...</option>
...

Naturally you can also use a CSS class rather than an in-line style.

If you are trying to control the height of the box that the options are
displayed inside, use the "size" attribute of the select element:

<form...>
<select style="font-size: 12pt;" size="4">
<option ...>...</option>
...

"size" refers to the number of options displayed, so you may not want
to use 500.

More information can be found here:

<URL: http://www.w3.org/TR/html4/interact/forms.html#h-17.6>

Cheers, Rob.


I don't want to set the font size of the select size. I want to set
the height of each option in the select, like line height.
Jul 23 '05 #9
RobG <rg***@iinet.net.auau> wrote in message news:<GQ*****************@news.optus.net.au>...
RobB wrote:
RobG wrote
You control the style of an option from the select element it's nested
inside.

Only in Internet Explorer, where 'windowed' elements (like listboxes)
have limited exposure. More advanced CSS browsers allow styling of
Options like any other element, although some layout aspects,
naturally, pertain to the containing list rather than any specific
option.


Not just Explorer, Safari is the same (but I haven't tested the latest
version). I expect all the Geko-based browsers support styles on
options, perhaps someone can comment on Opera.

Below is some test code, try changing the height of option 11 to 20em -
additional space is added below option 25 - similarly if you make it
500px as the OP attempted.

A bit of testing in Firefox shows that whilst small heights (say
equivalent to 2 to 5 lines) work fine, large heights are not reliable
and results vary if options have different heights specified. Note
also that the option height affects the select height - even though
only 3 rows are supposed to show, the select is greatly expanded - I
guess that the select height is set to size x the largest line height
which can result in an overly extended select.

Given that the OP didn't see any effect of "height" in their browser,
it's likely that s/he isn't using one of the "more advanced CSS
browsers". And given the majority of web surfers use IE, it's best not
to depend on features it doesn't have.

To top it off, the difficulty of controlling the size of the select
element itself if options are given different heights introduces
apparent unreliability in the UI that users may not understand.

But it's an issue worth discussing - thanks for bringing it up. :-)

Rob.

*Play code follows:*

<form action="">
<select name="aSelect"
style="font-size: 10pt; font-family: sans-serif;" size="3">
<option value="opt1">option 1
<option value="opt2">option 2
<option value="opt3">option 3
<option value="opt4">option 4
<option value="opt5">option 5
<option value="opt6">option 6
<option value="opt7">option 7
<option value="opt8"
style="font-family: courier; font-weight: bold;">option 8
<option value="opt9"
style="font-weight: bold; height: 20px;">option 9
<option value="opt10">option 10
<option value="opt11"
style="font-weight: bold; height: 2em;">option 11
<option value="opt12">option 12
<option value="opt13">option 13
<option value="opt14">option 14
<option value="opt15">option 15
<option value="opt16">option 16
<option value="opt17">option 17
<option value="opt18">option 18
<option value="opt19">option 19
<option value="opt20">option 20
<option value="opt21">option 21
<option value="opt22">option 22
<option value="opt23">option 23
<option value="opt24">option 24
<option value="opt25">option 25
</select><br>
<input type="reset"><br><br>
<input type="text" cols="10" name="aNum">
Enter a number from 1 to 25 and that option will be selected<br>
<input type="button" value="click me" onclick="
var x = this.form.aNum.value;
if (x > 0 && x < 26) {
this.form.aSelect.options[x - 1].selected='true'
} else {
alert('From 1 to 25 please, ' + x + ' is out of bounds');
}
">
</form>


Thanks, that makes sense. So setting the height of option isn't
something that can be done in IE. And sure enough, testing my existing
code in Firefox works as expected, sadly it's for an internal web
application where only IE is supported.

Oh well, another feature dropped from the supported list.
Jul 23 '05 #10

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

Similar topics

4
by: Jeff Anderson | last post by:
Is there a style for a select option's "selected" color? For example: <HTML> <BODY> <FORM NAME="form1"> <SELECT NAME="mySelect" SIZE="7" style="background-color:red;"> <OPTION>Test 1...
3
by: Max Weber | last post by:
Try to run the code below in a page. You will notice than when you switch the multiple attribute of the SELECT tag, only one option is displayed as selected although multiple options have ben...
4
by: David | last post by:
Greeting All: Time to pick the collective brain cell again. I have a web page that adds options to a select box on the fly. What I need to do is set the onDblClick event the same way. I need...
9
by: Adam | last post by:
Hey, I'm trying to write a script with two standard drop down boxes. One contains days one contains the month. I want to update the options in the days box everytime the month is changed......
4
by: thomastk | last post by:
Hi, In the following script, I am trying to set selection to a select option element, that is newly created within the script. It works fine on IE installations on Windows 2000 and some XP...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
3
by: Jason Carlton | last post by:
Is there a way to change the font-family and font-size for each individual option in a select menu? Something along the lines of: <select size="1"> <option style="font-family: Arial; font-size:...
6
by: Steve | last post by:
I have a div with two - three paragrahs in it. Each paragraph has its own inline style tag with its own font size setting. When I set the last paragraph's font size the font sizes for ALL of...
1
by: jmartmem | last post by:
Greetings, I have a nagging problem with client-side dynamic dependent list boxes that perhaps someone can help me troubleshoot. I have a form with a series of dynamic dependent list boxes....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.