PDA

View Full Version : options[objTo.options[i].text] = true;


blindbull
08-14-2002, 03:17 PM
What does:

options[objTo.options[i].text] = true;

do?

TIA.
Richard.

RadarBob
08-14-2002, 03:37 PM
This sounds like a cop-out, but that line of code is not in context. So what does it do? It depends.

blindbull
08-15-2002, 09:24 AM
Sorry, here is the complete code.

If you know what the line does and why it doesn't work that I will be a happier man.

I am trying to move an item from one select box to another. It works and I get the item copied and can see this on the interface. However, when the form is posted and I go to retreive the select values no value is found. If I paste the code below into dreamweaver the values are not listed when I view the properties and the resulting HTML looks like this:

<select name=selPackName1 ondblclick='javascript: AddTemplate(main_form.selPackName2,main_form.selPackName1);'></select>

The size of the select box is 1. Here is the code:

function AddTemplateName(objTo,objFrom)
{

// This function adds the template name from one select to another.

// create an object to store the listbox items that are selected.
var options = new Object();
var i;

// loop through all elements in the listbox that we are copying to.
for (var i=0; i<objTo.options.length; i++)
{
options[objTo.options[i].text] = true;
}

var objExisting = objFrom.options[objFrom.selectedIndex];

objTo.options[objTo.options.length] = new Option( objExisting.text, objExisting.value, false, false);

// deselect the item.
objTo.selectedIndex = -1;
return true;
}

RadarBob
08-15-2002, 01:49 PM
First, this:
<select name=selPackName1
ondblclick='java script: AddTemplate(main_form.selPackName2,main_form.selPackName1);'>
</select>

Can be rewritten like this:
<select name=selPackName1
onclick= 'AddTemplate(this, main_form.selPakName2);'>
</select>

Essentially the select object is passing itself. And, assuming you have "AddTemplate" inside a <script language=javascript...> tag (of course you do), then you don't need the "javascript:" identifier either.

BTW is "ondblclick" a valid event for the select object?

I didn't check out your code so I don't know why it doesn't work. I'm not up on creating & using "generic" objects. Here's kinda how I did it in my code:


function AddTemplateName (fromList, toList) {
// this function assumes fromList is a single select, not multiple

toList.options[toList.length] = new Option (
fromList.options[fromList.selectedIndex].text,
fromList.options[fromList.selectedIndex].value,
false, false);
} // function AddTemplateName()


NOTES:
.text is what you see on the screen.
.value you don't see. It can be anything you need it to be.

The "false, false" is "is this selected by default?" and "is this selected now?"

This new <option> was created dynamically - without re-building the html page. So if you look at the source code you will not see it; but it's there.

"length" property tells you how many <option>s currently exist in the <select>object. Since referencing these is just like arrays in javascript - they start at zero, so "length" can be used as an index to create the very next <option> for any given list (<select> object). So if length == 2 that means there is a "toList.options[0]" & "toList.options[1]" already.