debugger wrote:
the error is "htmlfile: Could not set the selected property.
Unspecified error."
option element for drop down is created by:
for(.......)
{
var myEle = document.createElement("option");
myEle.value=some value;
myEle.innerHTML= some string;
Yuck. Why use innerHTML?
myEle.appendChild(document.createTextNode('some string'));
document.getElementById("someDropDown").appendChil d(myEle);
Do some feature detection before using gEBI (and you should
probably include a document.all method too if using gEBI):
if (document.getElementById) {
document.getElementById("someDropDown")...;
...
Or even better, use the forms collection:
document.forms['someDropDown']...;
Avoids gEBI entirely - but see notes below.
}
after this, there is other for loop setting the selected property.
code causing the error:
for(var k=0; k<something; k++)
{
if(somecodition)
{
var mySelectedOption =
document.getElementById("someDropDown").options[k];
mySelectedOption.selected=true; <<<<<<<<<<<<======== error
occurs here
Why not keep a reference to "someDropDown" when you access it
above, then use it again when referencing the option:
var myDropDown = document.forms['someDropDown'];
myDropDown.appendChild(myEle);
...
myDropDown.options[k].selected = true;
Now there is no need for the second gEBI (or if the forms
collection is used, no need for gEBI at all)
[...] now when i put alert in the above "if statement", the error does not
occur.
Whilst Firefox does not generate an error, IE does. The error
is not fatal and the script appears to execute properly - the
option is added and selected. If users have not set IE to
report JavaScript errors, they will not see any error and the
script works fine. I would call this a bug in IE.
You can use setAttribute to keep IE happy, but then Firefox will
not select the option. Given that browser detection should only
be used as an absolute last resort, this would seem an
appropriate place to use try/catch:
if ( i == len-1){
try {
mySel.options[i].selected = true;
} catch(e) {
mySel.options[i].setAttribute('selected', true);
}
}
In future, please post code that actually replicates the error,
it makes the job of suggesting fixes much easier.
The following code implements the suggestions above and fixes
the error.
I still don't understand why you don't set myEle (the option) to
selected when you create it, that way works in both Firefox and
IE without the try/catch nastiness.
<html><head><title>play</title>
</head><body>
<script type="text/javascript">
function doStuff() {
var mySel = document.forms['aForm'].elements['someDropDown'];
var myEle = document.createElement('option');
myEle.value= 'someValue';
myEle.appendChild(document.createTextNode('some string'));
// why not set it to selected here?
// myEle.selected = true;
mySel.appendChild(myEle);
// if set to selected above, this whole block is not needed.
var len = mySel.length;
for (var i=0; i<len; i++) {
if ( i == len-1){
try {
mySel.options[i].selected = true;
} catch(e) {
mySel.options[i].setAttribute('selected', true);
}
}
}
}
</script>
</head><body>
<form action="" name="aForm">
<select name="someDropDown" id="someDropDown">
<option value="steve">Steve</option>
<option value="harry">Harry</option>
<option value="sue">Sue</option>
</select>
<br>
<input type="button" onclick="doStuff();" value="Click">
</form>
</body></html>
--
Rob