Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > DOM select onchange javascript

Reply
Thread Tools

DOM select onchange javascript

 
 
donald
Guest
Posts: n/a
 
      07-30-2006
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[i] = new Option(list[i],'');
}
};

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      07-31-2006
donald wrote:
> 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/ >


--
Rob

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
<HTML:SELECT onChange="onSelection();"> HELP!!!!!! Anders S. Clausen Java 3 01-05-2009 08:13 AM
Dynamic Select and onChange Michael McGrew ASP General 3 12-14-2006 12:14 PM
Problem - Parsing information using onchange in two select fields please-answer-here ASP General 2 05-30-2005 11:52 PM
select of select box will select multiple in another box palmiere Javascript 1 02-09-2004 01:11 PM
Refreshing a href variable using OnChange of a select list Paul Eghbal ASP General 2 09-15-2003 10:28 PM



Advertisments