DOM select onchange javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • donald

    DOM select onchange javascript

    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(typena me,itemname)
    {
    if (!optionTest) return;
    var type = document.forms[formname].elements[typename];
    var number = type.options[type.selectedIn dex].value;
    alert(number);
    if (!number) return;

    var list = items[number];
    var item = document.forms[formname].elements[itemname];
    item.options.le ngth = 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],'');
    }
    };

  • Jeff

    #2
    Re: DOM select onchange javascript

    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 = ..
    >
    What do I made it equal to for the above. if the typename = 'type1' and
    itemname = 'item1'.
    Forgive for asking what seems to me to be the obvious. Why change the
    onchange function when you can alter what that function does?

    <select onchange="doThi s()">
    ....

    var global_flag;

    function doThis(){
    if(global_flag == 'some_value'){d o this...}else{do that...}
    }

    Jeff
    >
    Can I do setAttribute or some think??
    >
    Thanks
    >
    Donald
    >
    >
    >
    function populate(typena me,itemname)
    {
    if (!optionTest) return;
    var type = document.forms[formname].elements[typename];
    var number = type.options[type.selectedIn dex].value;
    alert(number);
    if (!number) return;
    >
    var list = items[number];
    var item = document.forms[formname].elements[itemname];
    item.options.le ngth = 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],'');
    }
    };
    >

    Comment

    • RobG

      #3
      Re: DOM select onchange javascript

      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(pa rm1, 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('t ype1', 'item1');
      }

      Is that what you want? You can do:

      object.onchange = function(){
      var param1 = 'type1';
      var param2 = 'item1';
      someFunction(pa ram1, 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(typena me,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.selectedIn dex].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

      Comment

      Working...