Hi !
I have a problem with Internet Explorer that I really can't explain.
If anyone can find a solution to this, I congratulate you...
I'm trying to have a script dynamically select all (or some) of the
options in a select field. It won't work, it will only select one
option at a time...
While I was trying to debug, I realized that if I put an alert before
the instructions, it will work !!!
I've been able to recreate the problem in a simple html file... try
this !
First run the file as it is here... one option is selected.
Then, uncomment the alert("test"); line and run it again... all
options are selected !!!!!!!
Thanks for any help...
---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<span id="itGoesHere"></span>
</body>
</html>
<script type="text/javascript">
function mySelectField(optionsToCreate) {
this.fieldReference = window.document.createElement("select");
this.fieldReference.id = "myselect";
this.fieldReference.multiple = true;
this.fieldReference.size = 10;
window.document.getElementById("itGoesHere").appendChild(this.fieldReference);
for (var i=0; i < optionsToCreate.length; i++) {
this.fieldReference.options[this.fieldReference.options.length] = new
Option(optionsToCreate[i][1], optionsToCreate[i][0]);
}
}
mySelectField.prototype.selectAll = function() {
for (var i=0; i < this.fieldReference.options.length; i++)
{
this.fieldReference.options[i].selected = true;
}
}
var myField = new mySelectField([["1", "Test - 1"],["2", "Test -
2"],["3", "Test - 3"],["4", "Test - 4"],["5", "Test - 5"]]);
//alert("test");
myField.selectAll();
</script>
Danny - 29 Jul 2006 06:52 GMT
Roughly I'd say is a runtime issue in IE, so, do the appendChild() first, and
the .multiple=true; afterwards, as for selecting options, new Option() constructor has 4
optional arguments, the last 2 are also boolean, true/false if its default state is to
selected , and true/false for current state to selected, which you can use when making the
option, instead of looping through.
Danny
DBoy001 - 31 Jul 2006 15:39 GMT
Hmmm.. nope ! I've tried both Ideas and it did nothing good...
I'm wondering if there might be something I could put instead of an
alert that would cause it to magically work without requiring user
input...
DBoy001 - 31 Jul 2006 16:10 GMT
Found an answer on another thread of this forum (aparently, lots of
people have experienced this). If I create the select using this
format, it'll work :
this.fieldReference = window.document.createElement("<select
name=\"myselect\" id=\"myselect\" multiple=\"multiple\"
size=\"10\"></select>");
Paul - 31 Jul 2006 19:09 GMT
> Hi !
>
[quoted text clipped - 57 lines]
> myField.selectAll();
> </script>
Kindof lame but, putting the last line in a timeout for a delay made it
work on mine.
setTimeout("myField.selectAll();",10);