options.selected on select multiple

D

DBoy001

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[1], optionsToCreate[0]);
}
}
mySelectField.prototype.selectAll = function() {
for (var i=0; i < this.fieldReference.options.length; i++)
{
this.fieldReference.options.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>
 
D

Danny

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
 
D

DBoy001

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...
 
D

DBoy001

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>");
 
P

Paul

DBoy001 said:
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[1], optionsToCreate[0]);
}
}
mySelectField.prototype.selectAll = function() {
for (var i=0; i < this.fieldReference.options.length; i++)
{
this.fieldReference.options.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>

Kindof lame but, putting the last line in a timeout for a delay made it
work on mine.
setTimeout("myField.selectAll();",10);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top