473,657 Members | 2,594 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getEle mentById("selec t2");
var optGrp = document.create Element("optgro up");
optGrp.label="o pt2";
theSel.appendCh ild(optGrp);
var item = new Option();
optGrp.appendCh ild(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 5354
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.getEle mentById("selec t2");
var optGrp = document.create Element("optgro up");
optGrp.label="o pt2";
theSel.appendCh ild(optGrp);
[...]
<select size=8 id=select2>
</select>
Try <http://validator.w3.or g/#validate_by_in put+with_option sfor 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.de mon.co.uk>
Jun 27 '08 #2
On 2008-05-01, Thomas 'PointedEars' Lahn <Po*********@we b.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.getEle mentById("selec t2");
var optGrp = document.create Element("optgro up");
optGrp.label=" opt2";
theSel.appendC hild(optGrp);
[...]
<select size=8 id=select2>
</select>

Try <http://validator.w3.or g/#validate_by_in put+with_option sfor 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
removeFirstChil d() 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 removeFirstChil d 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.getEle mentById("selec t2");
var optGrp = document.create Element("optgro up");
optGrp.label="o pt2";
theSel.appendCh ild(optGrp);
var item = new Option();
optGrp.appendCh ild(item);
item.value = "bar";
item.text = "bar";
theSel.removeCh ild(theSel.firs tChild);
theSel.removeCh ild(theSel.firs tChild);
}
</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
removeFirstChil d() 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 removeFirstChil d 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
7266
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 connection. Further, the Sybase server shows no connection. Because the page is hanging I am unable, with my limited knowledge, to query the error collection. After many tries I will get an error page ...HTTP 403.9 Access forbidden: Too many users are...
5
35133
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 the options using OPTGROUPs. <select name="Selector1" size="1" onchange="reload2(this.options.selectedIndex, document.Form1.Selector2)"> <option ..... </select>
10
7638
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 the onchange event isn't called when mouse wheel scrolling between A3 and B1, but it works properly when scrolling between A1 and A2. E.g. ------------------------------------------
3
15610
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 only <option> tags is fairly easy and there is plenty of material on the net to assist with this. However, I have searched long and found the material on the subject of <optgroup> to be sparse, with a few posts here & there, but basically you're...
1
2787
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 but not IE. Yet this link shows Microsoft advertising that the disabled attribute is supported by them.
1
4609
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 can give each optgroup individual ID, e.g. id="group1"..."group"17). e.g. the html code looks like: <select name="mySelect" id="mySelect"> <optgroup label="First group" id="group1" > <option value="a" > Item A
1
3609
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 control marking the select as runat="server" raises an System.Web.HttpException: 'HtmlSelect' cannot have children of type 'LiteralControl'. I'm paging through search results but I'm also wondering if anybody has a clever hack yet? <%=...
5
5758
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 select box based on the user selected option of the first select box. It works fine in Mozilla but IE7 still shows all the optgroups in the second select box. The idea is to show the appropriate list of states based on what country a user selects...
2
3260
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 goofiness of the current look. The idea is a new month will be added to the list each month (hence two option groups right in a row. But what is also missing is the closing tag for the 2008 options group. So I need examples for the start and close...
0
8305
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8503
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
8605
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...
1
6163
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...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1607
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.