DOM select onchange javascript

D

donald

I have a function called populate which populate a select box. I need
it to run when a different select box value is chnage. So I need the
event onChange.

But i need to pass it two var to. how do i do this??

object.onchange = ..

What do I made it equal to for the above. if the typename = 'type1' and
itemname = 'item1'.

Can I do setAttribute or some think??

Thanks

Donald



function populate(typename,itemname)
{
if (!optionTest) return;
var type = document.forms[formname].elements[typename];
var number = type.options[type.selectedIndex].value;
alert(number);
if (!number) return;

var list = items[number];
var item = document.forms[formname].elements[itemname];
item.options.length = 0;
if (number == 0) {
item.options[0] = new Option('Pick Item ---->','');
alert('');
return;
}
for(i=0;i<list.length;i+=1)
{
item.options = new Option(list,'');
}
};
 
R

RobG

donald said:
I have a function called populate which populate a select box. I need
it to run when a different select box value is chnage. So I need the
event onChange.

But i need to pass it two var to. how do i do this??

object.onchange = ..

Presumably you want to know how to dynamically set an onchange handler
using a function that requires parameters, so:

object.onchange = function(){
someFunction(parm1, parm2);
}


Your next question will be 'how do I set the values of parm1 and
parm2?'.

What do I made it equal to for the above. if the typename = 'type1' and
itemname = 'item1'.

object.onchange = function(){
someFunction('type1', 'item1');
}

Is that what you want? You can do:

object.onchange = function(){
var param1 = 'type1';
var param2 = 'item1';
someFunction(param1, param2);
}


That creates a closure (in this case a completely unnecessary one) back
to the anonymous function, which may have complications.

Can I do setAttribute or some think?? [...]
function populate(typename,itemname)

Please indent code with 2 (preferred) or 4 spaces and manually wrap at
about 70 characters. Allow for a couple of quoted replies too.

{
if (!optionTest) return;

Where is optionTest declared or initialised? It doesn't seem to get
used anyway.

var type = document.forms[formname].elements[typename];

Where is formname initialised?

var number = type.options[type.selectedIndex].value;
alert(number);
if (!number) return;

var list = items[number];

Where was items initialised?

[...]

You are better to post an example or link that is a minimal example of
what you are trying to do. Search the archives for 'dependent select
options' or 'dynamic select list'.

Try:
<URL: http://www.javascripttoolbox.com/lib/dynamicoptionlist/ >
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top