sending array to function parameter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jgentes
    New Member
    • Jul 2007
    • 32

    sending array to function parameter

    I know its possible, but I haven't been able to get a hold of how to send the parameter as an array or string?

    Code:
    function updateHours(el, val)
    {
         if(el!=null && val!=null)
         {
              var element = document.getElementById(el);
              element.innerHTML = element.value + val;
         }
    }
    is what I have right now and it will work as long as the element is a single string.
    to make this robust, I want to be able to insert an array into both parameters or just 1 parameter... so it would update accordingly

    Code:
    function updateHours("['el1','el2','el3']", "['val1','val2','val3']")
    {
    }
    would update 3 elements taking into account the appropriate 3 values and

    Code:
    function updateHours("['el1','el2','el3']", "val")
    {
    }
    would update all 3 elements taking into account only 1 value.


    Code:
    function updateHours("el", "['val1','val2','val3']")
    {
    }
    would update 1 element with an assortment of values
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Pass the array as ['el1','el2','el 3'] and use a for loop to loop over the elements, e.g.
    [code=javascript]for (i=0; i<el.length; i++) {
    var element = document.getEle mentById(el[i]);
    element.value = element.value + val;
    }[/code]

    Comment

    Working...