473,397 Members | 2,056 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,397 software developers and data experts.

Optgroup causes blank line in Select; Firefox but not IE

I am using javascript to manipulate optgroups in select elements.
When the results are displayed in Firefox 2.0 there is an
annoying blank line at the top of the multi-line select box.
This doesn't happen with IE7.

There's a little program below which illustrates the problem.
I create two multi-line select boxes. The first one has an
optgroup and an option specified in the HTML. That one displays
with "opt1" as the first line. The second select is populated
using appendChild(). As far as I know the results should be
the same.

Can anyone explain this?
----------------------------------------------
<html>
<head>
<script>
function addOpt() {
var theSel = document.getElementById("select2");
var optGrp = document.createElement("optgroup");
optGrp.label="opt2";
theSel.appendChild(optGrp);
var item = new Option();
optGrp.appendChild(item);
item.value = "bar";
item.text = "bar";
}
</script>
</head>
<body onload="addOpt();">
<select size=8 id=select1>
<optgroup label=opt1>
<option value="foo">foo
</optgroup>
</select>
<select size=8 id=select2>
</select>
</body>
</html>
Jun 27 '08 #1
4 5342
Patrick Nolan wrote:
I am using javascript to manipulate optgroups in select elements.
When the results are displayed in Firefox 2.0 there is an
annoying blank line at the top of the multi-line select box.
This doesn't happen with IE7.

There's a little program below which illustrates the problem.
I create two multi-line select boxes. The first one has an
optgroup and an option specified in the HTML. That one displays
with "opt1" as the first line. The second select is populated
using appendChild(). As far as I know the results should be
the same.

Can anyone explain this?

[...]
var theSel = document.getElementById("select2");
var optGrp = document.createElement("optgroup");
optGrp.label="opt2";
theSel.appendChild(optGrp);
[...]
<select size=8 id=select2>
</select>
Try <http://validator.w3.org/#validate_by_input+with_optionsfor a start.

WFM if the `select' element is dynamically generated as well.

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404
Firefox/2.0.0.14
HTH

PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Jun 27 '08 #2
On 2008-05-01, Thomas 'PointedEars' Lahn <Po*********@web.dewrote:
Patrick Nolan wrote:
>I am using javascript to manipulate optgroups in select elements.
When the results are displayed in Firefox 2.0 there is an
annoying blank line at the top of the multi-line select box.
This doesn't happen with IE7.

There's a little program below which illustrates the problem.
I create two multi-line select boxes. The first one has an
optgroup and an option specified in the HTML. That one displays
with "opt1" as the first line. The second select is populated
using appendChild(). As far as I know the results should be
the same.

Can anyone explain this?

[...]
var theSel = document.getElementById("select2");
var optGrp = document.createElement("optgroup");
optGrp.label="opt2";
theSel.appendChild(optGrp);
[...]
<select size=8 id=select2>
</select>

Try <http://validator.w3.org/#validate_by_input+with_optionsfor a start.

WFM if the `select' element is dynamically generated as well.
OK. I learned that any SELECT element created in HTML must contain at
least one OPTION. So I put in a dummy OPTION, intending to remove it
in my onload handler. I don't understand the result. I tried
removeFirstChild() at the beginning of the handler, but there were no
OPTION elements to find at that point.

Finally I found a kluge that works. I add the new OPTGROUP and OPTION
in the onload handler, then removeFirstChild twice to get rid of the
dummy OPTION. This produces the desired effect, but I certainly don't
know why. The new script is below.

By the way, I figured out how to install the Firefox DOM inspector.
It didn't help. In the first version of the script, it showed a #text
element (with no content) before the OPTGROUP in both SELECTs. There
was no clue why Firefox should handle them differently.

--------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title>Select element with blank line</title>
<script type="text/javascript">
function addOpt() {
var theSel = document.getElementById("select2");
var optGrp = document.createElement("optgroup");
optGrp.label="opt2";
theSel.appendChild(optGrp);
var item = new Option();
optGrp.appendChild(item);
item.value = "bar";
item.text = "bar";
theSel.removeChild(theSel.firstChild);
theSel.removeChild(theSel.firstChild);
}
</script>
</head>
<body onload="addOpt();">
<select size=8 id=select1>
<optgroup label=opt1>
<option value="foo">foo
</optgroup>
</select>
<select size=8 id=select2>
<option value="bar">bar
</select>
</body>
</html>
Jun 27 '08 #3
*** Patrick Nolan escribió/wrote (Thu, 1 May 2008 17:04:16 +0000 (UTC)):
I am using javascript to manipulate optgroups in select elements.
When the results are displayed in Firefox 2.0 there is an
annoying blank line at the top of the multi-line select box.
This doesn't happen with IE7.
It works fine in all other browsers I've tested: Opera 9, Konqueror 3.5 and
even Firefox 3 beta 5. Furthermore, if you obtain the generated HTML and
paste it on the page as plain HTML the <selectis rendered fine.

I'd dare say you've hit a bug in Firefox 2.
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor en cubitos: http://www.demogracia.com
--
Jun 27 '08 #4
Patrick Nolan wrote:
OK. I learned that any SELECT element created in HTML must contain at
least one OPTION. So I put in a dummy OPTION, intending to remove it in
my onload handler. I don't understand the result. I tried
removeFirstChild() at the beginning of the handler, but there were no
OPTION elements to find at that point.
Because there is a whitespace text node that is the first child node.
Finally I found a kluge that works. I add the new OPTGROUP and OPTION in
the onload handler, then removeFirstChild twice to get rid of the dummy
OPTION. This produces the desired effect, but I certainly don't know
why. The new script is below.
You have removed the whitespace text node, and then its `option' sibling.
By the way, I figured out how to install the Firefox DOM inspector. It
didn't help. In the first version of the script, it showed a #text
element (with no content)
This is not entirely true. The content are whitespace characters,
presumably newline followed by a number of spaces.
before the OPTGROUP in both SELECTs. There was no clue why Firefox
should handle them differently.
The reason is that Firefox/Gecko is standards-compliant in that regard,
while IE/Trident seemingly randomly removes whitespace text nodes from the
document tree, and on the other hand inserts them where they don't belong
(this leads to certain display bugs e.g. with lists).

While it is a possibility to remove the whitespace in the markup, you should
create the entire `select' element with client-side scripting instead, as I
said before.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Jun 27 '08 #5

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

Similar topics

6
by: Paul Robinson | last post by:
I am developing a website in ASP that connects to a Sybase database. However, when I try to open a connection to the database the page will not load. The script does not timeout, nor the...
5
by: cedawe | last post by:
I have two select boxes. When the user picks a value in the first one it completely re-populates the second one. It works fine, but only generates a standard OPTIONS list and I now want to group...
10
by: Ryan McGeary | last post by:
In a <select> drop-down, the onchange event isn't called when scrolling through the dropdown using the mouse-wheel and when crossing over a new <optgroup>. Using the example below, notice how...
3
by: Stewart | last post by:
Dear comp.lang.javascript, I have more than once wanted to manipulate the contents of select boxes dynamically, whilst the boxes contain <optgroup> tags. Manipulation of a select box containing...
1
by: Stewart Cambridge London, UK | last post by:
I've been having some trouble with a set of <select><optgroup><option> dropdowns. I've been experimenting with the disabled attribute in the <optgroup> and <option> tags. Seems to work in NN...
1
by: TKapler | last post by:
I think i am quite experienced javascript programmer, but I got a problem. I have a selectbox with e.g. 17 optgroups with 100 options. I need a javascript code to hide some of that optgroups (i...
1
by: clintonG | last post by:
IE 6 finally supported HTML 4.0 optgroup used with the select list lemet but there's no apparent way to use optgroup with ASP.NET. The DropDownListBox will not support optgroup and writing an HTML...
5
by: jhappeal | last post by:
I do not know Javascript that well so I might be going about this the wrong way. Any help would be appreciated. This function attempts to hide the options inside of the optgroup tag of the second...
2
Claus Mygind
by: Claus Mygind | last post by:
I am adding a select box on the fly to a menubar. For the most part straight forward. Just one little hangup. I want to add the Option Group. Not sure how that should be coded. Don't mind the...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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...

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.