P
Peter X
Hi all,
First appolgies for posting a mostly IE5.5 question here, but the IE5.5
groups are a bit dead these days. Hopefully someone here can help me!
I'm using <optgroup> tags with <select> and <option> to group related
options, e.g.:
<select name="myselect">
<optgroup label="first group">
<option>one</option>
<option>two</option>
</optgroup>
<optgroup label="second group">
<option>three</option>
<option>four</option>
</optgroup>
</select>
but as we know, older browsers won't show the group names so anything
before IE6 wont' show. So I wrote the following script:
function fixOptgroup() {
var optGroups, optGroup, newOption, newOptionText;
optGroups = document.getElementsByTagName("optgroup");
for(var i=0; i<optGroups.length; i++) {
optGroup = optGroups;
newOption = document.createElement("option");
newOption.setAttribute("value", "");
newOption.setAttribute("class", "optgroup");
newOptionText = document.createTextNode(
optGroup.getAttribute("label") );
newOption.appendChild(newOptionText);
optGroup.insertBefore(newOption, optGroup.firstChild);
}
}
which finds all the "optgroup" elements, and inserts a new "select" as
the first child. It works fine in IE6** (and Mozilla) but I don't need
it to work there!
It fails in IE5.5 though saying "Unexpected call to method or property
access". As far as I can tell, this is because although
getElementsByTagName has retrieved a reference to the "optgroup" tags,
the firstChild property of all of these is null!
Does anyone have any ideas on how I should proceed?!
Thanks in advance!
Peter.
** [obviously in live use this code would only be called after browser
sniffing first]
First appolgies for posting a mostly IE5.5 question here, but the IE5.5
groups are a bit dead these days. Hopefully someone here can help me!
I'm using <optgroup> tags with <select> and <option> to group related
options, e.g.:
<select name="myselect">
<optgroup label="first group">
<option>one</option>
<option>two</option>
</optgroup>
<optgroup label="second group">
<option>three</option>
<option>four</option>
</optgroup>
</select>
but as we know, older browsers won't show the group names so anything
before IE6 wont' show. So I wrote the following script:
function fixOptgroup() {
var optGroups, optGroup, newOption, newOptionText;
optGroups = document.getElementsByTagName("optgroup");
for(var i=0; i<optGroups.length; i++) {
optGroup = optGroups;
newOption = document.createElement("option");
newOption.setAttribute("value", "");
newOption.setAttribute("class", "optgroup");
newOptionText = document.createTextNode(
optGroup.getAttribute("label") );
newOption.appendChild(newOptionText);
optGroup.insertBefore(newOption, optGroup.firstChild);
}
}
which finds all the "optgroup" elements, and inserts a new "select" as
the first child. It works fine in IE6** (and Mozilla) but I don't need
it to work there!
It fails in IE5.5 though saying "Unexpected call to method or property
access". As far as I can tell, this is because although
getElementsByTagName has retrieved a reference to the "optgroup" tags,
the firstChild property of all of these is null!
Does anyone have any ideas on how I should proceed?!
Thanks in advance!
Peter.
** [obviously in live use this code would only be called after browser
sniffing first]