View Full Version : Excessive Looping

04-25-2004, 04:07 AM
Why does this loop more than the specified amount of iterations? I have a feeling that I'm overlooking a minor type or the like, but it is really starting to bug me that I can't find it. value is equal to 2.

function addMe(e){
var list = document.daForm.listItems; // var list references the <select> for this function
var value = list.options[e].value;
var underS = '';

for(i = 0; i < (value + 2); i++){ // adds the right # of underscores to child
underS += '_';

04-25-2004, 03:05 PM
Simple! The type of the value property of form controls is string. Thus, what you are doing is that you're adding a numerical 2 to the string '2', thus generating the string '22'. That string is then autocast into a number by the < operator, thus resulting in numerical 22.

04-26-2004, 01:32 AM
Lol, I knew that it was something simple. Ahh the wonders of being a javascript newbie :D Another question while I'm at it. If I select more than one thing in the select field, how can I tell what has been selected? Also, is it possible to somehow enter in 2 spaces before an item in the select box so that it is indented. I have attempted to use ' ' and nbsp; but to no avail.

04-26-2004, 02:56 AM
If the type of combobox is set as multiple, you have to loop through the option items and check the selected property to see if it's true or false.

for (var i=0;i<refToCombo.options.length;i++){
if (refToCombo.options[i].selected) {
//item i is selected

In your second question, if you mean adding 2 spaces to the option item programmatically, then try using \u00a0\u00a0

04-26-2004, 05:09 PM
Any way to do it unprogrammatically[even a word?]?

04-26-2004, 05:21 PM
using &nsbp;
in select do make a space.

04-26-2004, 10:53 PM
You're right! I wonder what I was trying before. I probably forgot the '&'. Thank you Garadon.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum