DOM select onchange javascript

Discussion in 'Javascript' started by donald, Jul 30, 2006.

  1. donald

    donald Guest

    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,'');
    }
    };
    donald, Jul 30, 2006
    #1
    1. Advertising

  2. donald

    RobG Guest

    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
    RobG, Jul 31, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anders S. Clausen
    Replies:
    3
    Views:
    55,336
    ranjithchinnasamy83
    Jan 5, 2009
  2. Paul Eghbal
    Replies:
    2
    Views:
    122
    GIMME
    Sep 15, 2003
  3. please-answer-here
    Replies:
    2
    Views:
    110
    please-answer-here
    May 31, 2005
  4. Michael McGrew

    Dynamic Select and onChange

    Michael McGrew, Dec 13, 2006, in forum: ASP General
    Replies:
    3
    Views:
    285
    Anthony Jones
    Dec 14, 2006
  5. palmiere
    Replies:
    1
    Views:
    396
    Erwin Moller
    Feb 9, 2004
Loading...

Share This Page