Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_web_howto thread: Starting At Index 0 in List Box


Message #1 by "Rita Greenberg" <rg1@h...> on Mon, 26 Mar 2001 21:44:51
Hi.



I dynamically populate a list box. My problem is that the first item in 

the list box starts at Index 1. I want it to start at Index 0. 



Here's my code:



<script language="JavaScript">

<!--

function addtolist() 

{

	/* Find selected items in list box1 */

	var cSelect = document.all.item("oGroups");		/* List 

box1 */

	var cOut = document.all.item("oSelected");		/* List 

box2 */

	

	for (var i=0; i<cSelect.options.length; i++) {

		/* Check to see if a particular option is selected from 

list box 1*/

		if(cSelect.options(i).selected) {

			/* Add new option to "Groups Selected" list box2 */

			 cOut.options[cOut.options.length]= new Option

(cSelect.options(i).text, "value " + i);  

		}			 

	}

}

-->

</script>



How can I get the first item in list box2 to start at Index 0?



Rita
Message #2 by "George Draper" <georgedraper@m...> on Mon, 26 Mar 2001 22:27:19 -0500
I'm not sure this solves your problem, but it looks like you only want to

increment your index when the if statement is true. If that's the case use a

second index, ii, and increment it inside the if statement.





var ii = 0;

for (var i=0; i<cSelect.options.length; i++) {

/* Check to see if a particular option is selected from

list box 1*/

if(cSelect.options(i).selected) {

/* Add new option to "Groups Selected" list box2 */

cOut.options[cOut.options.length]= new Option

(cSelect.options(ii).text, "value " + ii);

ii++;

}



Good luck.



- George





-----Original Message-----

From: Rita Greenberg <rg1@h...>

To: ASP Web HowTo <asp_web_howto@p...>

Date: Monday, March 26, 2001 3:51 PM

Subject: [asp_web_howto] Starting At Index 0 in List Box





>Hi.

>

>I dynamically populate a list box. My problem is that the first item in

>the list box starts at Index 1. I want it to start at Index 0.

>

>Here's my code:

>

><script language="JavaScript">

><!--

>function addtolist()

>{

> /* Find selected items in list box1 */

> var cSelect = document.all.item("oGroups"); /* List

>box1 */

> var cOut = document.all.item("oSelected"); /* List

>box2 */

>

> for (var i=0; i<cSelect.options.length; i++) {

> /* Check to see if a particular option is selected from

>list box 1*/

> if(cSelect.options(i).selected) {

> /* Add new option to "Groups Selected" list box2 */

> cOut.options[cOut.options.length]= new Option

>(cSelect.options(i).text, "value " + i);

> }

> }

>}

>-->

></script>

>

>How can I get the first item in list box2 to start at Index 0?

>

>Rita







Message #3 by Rita Greenberg <rg1@h...> on Tue, 27 Mar 2001 07:37:05 -0800
Hi George.



I tried using a second index but that didn't work, it still started at Index

1. It also gave me the wrong index selected from list box1. 



Thanks, Rita



-----Original Message-----

From: George Draper [mailto:georgedraper@m...]

Sent: Monday, March 26, 2001 7:27 PM

To: ASP Web HowTo

Subject: [asp_web_howto] Re: Starting At Index 0 in List Box





I'm not sure this solves your problem, but it looks like you only want to

increment your index when the if statement is true. If that's the case use a

second index, ii, and increment it inside the if statement.





var ii = 0;

for (var i=0; i<cSelect.options.length; i++) {

/* Check to see if a particular option is selected from

list box 1*/

if(cSelect.options(i).selected) {

/* Add new option to "Groups Selected" list box2 */

cOut.options[cOut.options.length]= new Option

(cSelect.options(ii).text, "value " + ii);

ii++;

}



Good luck.



- George





-----Original Message-----

From: Rita Greenberg <rg1@h...>

To: ASP Web HowTo <asp_web_howto@p...>

Date: Monday, March 26, 2001 3:51 PM

Subject: [asp_web_howto] Starting At Index 0 in List Box





>Hi.

>

>I dynamically populate a list box. My problem is that the first item in

>the list box starts at Index 1. I want it to start at Index 0.

>

>Here's my code:

>

><script language="JavaScript">

><!--

>function addtolist()

>{

> /* Find selected items in list box1 */

> var cSelect = document.all.item("oGroups"); /* List

>box1 */

> var cOut = document.all.item("oSelected"); /* List

>box2 */

>

> for (var i=0; i<cSelect.options.length; i++) {

> /* Check to see if a particular option is selected from

>list box 1*/

> if(cSelect.options(i).selected) {

> /* Add new option to "Groups Selected" list box2 */

> cOut.options[cOut.options.length]= new Option

>(cSelect.options(i).text, "value " + i);

> }

> }

>}

>-->

></script>

>

>How can I get the first item in list box2 to start at Index 0?

>

>Rita






  Return to Index