Copy text and value between Select boxes

R

RobG

I found a couple of handy functions that will copy the text items from
one SELECT box to another (and back). This works great.

BUT... what I also need is the associated VALUE (from the options
value="whatever" tag)...

I tried this:

if (found != true) // if the option does not exist in destination
listbox, copy it over
{
destList.options[len] = new Option(srcList.options.text);
destList.options[len] = new Option(srcList.options.value); //
I added this line
len++;
}

But all it does is overwrite the text with the option.

How do you do it? It's probably something simple, but I sure can't
find it.

Thanks!

Rob
 
G

Gregor Kofler

RobG meinte:
I found a couple of handy functions that will copy the text items from
one SELECT box to another (and back). This works great.

BUT... what I also need is the associated VALUE (from the options
value="whatever" tag)...

I tried this:

if (found != true) // if the option does not exist in destination
listbox, copy it over
{
destList.options[len] = new Option(srcList.options.text);
destList.options[len] = new Option(srcList.options.value); //
I added this line
len++;
}

But all it does is overwrite the text with the option.

How do you do it? It's probably something simple, but I sure can't
find it.


Can't follow your "code" (what is "found")?
Anyway:

destList.options[len] = new Option(srcList.options.text,
srcList.options.value);

Gregor
 
S

SAM

RobG a écrit :
I found a couple of handy functions that will copy the text items from
one SELECT box to another (and back). This works great.


// if the option does not exist in destination listbox, copy it over
if (!found)
{
var o = new Option(srcList.options.text, srcList.options.value);
destList.options[destList.options.length] = o;
}
 
D

dhtml

RobG a écrit :
I found a couple of handy functions that will copy the text items from
one SELECT box to another (and back). This works great.

// if the option does not exist in destination listbox, copy it over
if (!found)
{
var o = new Option(srcList.options.text, srcList.options.value);
destList.options[destList.options.length] = o;
}


You don't actually need the Option constructor. Just increase the
select's - length - property and it automagically happens.
var selectOpts = document.getElementsByTagName('select')[0];

// Mock OPTION object.
var srcOpts = { name : "foo", value : 12, selected : true };

var len = selectOpts.length, option;
selectOpts.length++;
option = selectOpts[ len ];
option.text = srcOpts.name;
option.value = srcOpts.value;
// Set this defaultSelected for FireFox.
option.defaultSelected = option.selected = selectOpts.selected;


 
S

SAM

dhtml a écrit :
RobG a écrit :
I found a couple of handy functions that will copy the text items from
one SELECT box to another (and back). This works great.
// if the option does not exist in destination listbox, copy it over
if (!found)
{
var o = new Option(srcList.options.text, srcList.options.value);
destList.options[destList.options.length] = o;
}


You don't actually need the Option constructor.


what is the problem using Options constructor ?
Just increase the
select's - length - property and it automagically happens.

I think you haven't to increase the length
that's happens automatically doing
var o = destList.options[destList.length];
then
o.text = srcList.options.text;
o.value = srcList.options.value;

var selectOpts = document.getElementsByTagName('select')[0];

// Mock OPTION object.
var srcOpts = { name : "foo", value : 12, selected : true };

var len = selectOpts.length, option;
selectOpts.length++;
option = selectOpts[ len ];
option.text = srcOpts.name;
option.value = srcOpts.value;
// Set this defaultSelected for FireFox.
option.defaultSelected = option.selected = selectOpts.selected;
 
D

dhtml

dhtml a écrit :
RobG a écrit :
I found a couple of handy functions that will copy the text items from
one SELECT box to another (and back). This works great.
// if the option does not exist in destination listbox, copy it over
if (!found)
{
var o = new Option(srcList.options.text, srcList.options.value);
destList.options[destList.options.length] = o;
}

You don't actually need the Option constructor.

what is the problem using Options constructor ?

I just never needed to. Is it better to use the Option constructor?
Just increase the
select's - length - property and it automagically happens.

I think you haven't to increase the length
that's happens automatically doing
var o = destList.options[destList.length];

No, that doesn't increase the length, it just returns undefined.
then
o.text = srcList.options.text;


ReferenceError - o is undefined.


THe other thing about the magic length is that you can set it to 0 to
clear the list
 

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

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top