dynamic select box problem 
Author Message
 dynamic select box problem

Hi.  I'm trying to create a select box dynamically in an HTML page, add some
options, then select several of those options automatically.  I have code
that appears to do the right thing, but oddly it only works if I have an
"alert" box pop up after adding each of the options.  If I remove the alert
box, then only the last option I select is shown as selected.  Can someone
show me a way to make this work?  I don't mind cheesy workarounds!

My code is below... I'm using IE6 on XP professional, but this also happens
with IE6 on Win2K.  I haven't had the opportunity to test other platforms /
browsers.

Thanks,
Chris

<HTML>
<HEAD>
<script language="javascript">

function createSelect() {
 var sel = document.createElement("select");
 placeholder.appendChild(sel);
 sel.size = 10;
 sel.multiple = true;
 for (var i=0; i<10; i++) {
  sel.options[i] = new Option("Hello " + i);
  if (i%2 == 0) sel.options[i].selected = true;    // select every other
entry

  // comment out the alert below to make the selections fail
  alert('Hello ' + i);
 }

Quote:
}

</script>
</HEAD>
<BODY onload="createSelect();">

<div id="placeholder"></div>

</BODY>
</HTML>



Wed, 07 Jul 2004 04:24:17 GMT  
 dynamic select box problem

Quote:
> Hi.  I'm trying to create a select box dynamically in an HTML page, add
some
> options, then select several of those options automatically.  I have code
> that appears to do the right thing, but oddly it only works if I have an
> "alert" box pop up after adding each of the options.  If I remove the
alert
> box, then only the last option I select is shown as selected.  Can someone
> show me a way to make this work?  I don't mind cheesy workarounds!

I believe I've seen this error before. I'm having a lot of these deja-vu
experiences lately ;-).

Or maybe that should be :-(, since they're all bugs...

Try searching http://www.deja.com/ (hehehe, that's so funny) for earlier
posts.

You can probably use a window.setTimeout call instead of an alert -- the
browser just needs some "breathing room" before you go and update the next
element.

Peter

--

Waiting for the Vengabus? http://www.microsoft.com/info/cpyright.htm
Please post all questions to the group. Thanks.



Thu, 08 Jul 2004 06:02:28 GMT  
 dynamic select box problem
There's a Mac/IE specific bug that is triggered if the select.options is
ever empty. Other than that, don't know what the problem is. Try not making
it visible until you have added all the options to avoid the display
updating on every  iteration when you add/remove/edit.



Quote:


> > Hi.  I'm trying to create a select box dynamically in an HTML page, add
> some
> > options, then select several of those options automatically.  I have
code
> > that appears to do the right thing, but oddly it only works if I have an
> > "alert" box pop up after adding each of the options.  If I remove the
> alert
> > box, then only the last option I select is shown as selected.  Can
someone
> > show me a way to make this work?  I don't mind cheesy workarounds!

> I believe I've seen this error before. I'm having a lot of these deja-vu
> experiences lately ;-).

> Or maybe that should be :-(, since they're all bugs...

> Try searching http://www.deja.com/ (hehehe, that's so funny) for earlier
> posts.

> You can probably use a window.setTimeout call instead of an alert -- the
> browser just needs some "breathing room" before you go and update the next
> element.

> Peter

> --

> Waiting for the Vengabus? http://www.microsoft.com/info/cpyright.htm
> Please post all questions to the group. Thanks.



Thu, 08 Jul 2004 07:16:34 GMT  
 dynamic select box problem

Thanks for your suggestion... the setTimeout method works nicely.  Here's
what I have:

<HTML>
<HEAD>
<script language="javascript">

function createSelect() {
 var sel = document.createElement("select");
 placeholder.appendChild(sel);
 sel.id = "oSel";
 sel.size = 10;
 sel.multiple = true;
 for (var i=0; i<10; i++) {
  sel.options[i] = new Option("Hello " + i);
  if (i%2 == 0) setTimeout("setOption(" + i + ");", 1);

 }

Quote:
}

function setOption(number) {
 oSel.options[number].selected = true;

Quote:
}

</script>
</HEAD>
<BODY onload="createSelect();">

<div id="placeholder"></div>

</BODY>
</HTML>



Quote:


> > Hi.  I'm trying to create a select box dynamically in an HTML page, add
> some
> > options, then select several of those options automatically.  I have
code
> > that appears to do the right thing, but oddly it only works if I have an
> > "alert" box pop up after adding each of the options.  If I remove the
> alert
> > box, then only the last option I select is shown as selected.  Can
someone
> > show me a way to make this work?  I don't mind cheesy workarounds!

> I believe I've seen this error before. I'm having a lot of these deja-vu
> experiences lately ;-).

> Or maybe that should be :-(, since they're all bugs...

> Try searching http://www.deja.com/ (hehehe, that's so funny) for earlier
> posts.

> You can probably use a window.setTimeout call instead of an alert -- the
> browser just needs some "breathing room" before you go and update the next
> element.

> Peter

> --

> Waiting for the Vengabus? http://www.microsoft.com/info/cpyright.htm
> Please post all questions to the group. Thanks.



Sat, 10 Jul 2004 01:08:53 GMT  
 dynamic select box problem
You shouldnt have to mess around with alerts or timers to
get this to work...the following is standard code i use
for all me select elements (in this instance populating
the options with values from a remote script function).
It could be the code your using just aint quite right, or
I wondered if you had the placeholder in between a FORM
element, I find these can sometimes cause unwanted effects.
James.

        while (SelOptions.hasChildNodes())
        {
                SelOptions.removeChild
(SelOptions.childNodes[0]);
        }

        var co = RSExecute
(serverURL, "xmlGetVehicleOptions", xmlFileName,
SessionId);
        jsCheckReturnForError(co,
SessionId, "vehicleoptionsE10");

        var strString = co.return_value;
        var oOption;
        var strStringArray = strString.split(";");

        var maxlength   = 0;
        var spacers             = 0;

        for (n = 0; (n < strStringArray.length - 1); n++)
        {
                if (strStringArray[n].length > maxlength)
                {
                        maxlength = strStringArray
[n].length;
                }
        }
        maxlength += 5;

        for (n = 0; (n < strStringArray.length - 1); n++)
        {
                oOption = document.createElement("OPTION");

                oOption.value = strStringArray[n];
                n++;
                oOption.text= strStringArray[n];

                spacers = maxlength - strStringArray
[n].length;
                for (m = 0; m < spacers; m ++)
                {
                        oOption.text += " ";
                }

                oOption.text += strStringArray[n - 1];

                SelOptions.add(oOption);
        }

        SelOptions.selectedIndex = SelOptions.length - 1;

Quote:
>-----Original Message-----

>Hi.  I'm trying to create a select box dynamically in an
HTML page, add some
>options, then select several of those options

automatically.  I have code
Quote:
>that appears to do the right thing, but oddly it only
works if I have an
>"alert" box pop up after adding each of the options.  If
I remove the alert
>box, then only the last option I select is shown as

selected.  Can someone
Quote:
>show me a way to make this work?  I don't mind cheesy
workarounds!

>My code is below... I'm using IE6 on XP professional, but
this also happens
>with IE6 on Win2K.  I haven't had the opportunity to test
other platforms /
>browsers.

>Thanks,
>Chris

><HTML>
><HEAD>
><script language="javascript">

>function createSelect() {
> var sel = document.createElement("select");
> placeholder.appendChild(sel);
> sel.size = 10;
> sel.multiple = true;
> for (var i=0; i<10; i++) {
>  sel.options[i] = new Option("Hello " + i);
>  if (i%2 == 0) sel.options[i].selected = true;    //
select every other
>entry

>  // comment out the alert below to make the selections
fail
>  alert('Hello ' + i);
> }
>}

></script>
></HEAD>
><BODY onload="createSelect();">

><div id="placeholder"></div>

></BODY>
></HTML>

>.



Mon, 12 Jul 2004 01:37:31 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. How do you create Dynamic SELECT boxes?

2. Dynamic SELECT box in HTML

3. Dynamic Select box filling

4. Dynamic Javascript Menu's and Select Boxes

5. Problem with Dynamic Combo Boxes

6. <select> box problems

7. Select box problem

8. Select list box problem

9. Setting the selected option of a select box in code

10. Select all items in a multi-select box

11. Making a value selected in a select box.

12. dynamic select list

 

 
Powered by phpBB® Forum Software