By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,635 Members | 2,188 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,635 IT Pros & Developers. It's quick & easy.

sending array to function parameter

P: 32
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?

Expand|Select|Wrap|Line Numbers
  1. function updateHours(el, val)
  2. {
  3.      if(el!=null && val!=null)
  4.      {
  5.           var element = document.getElementById(el);
  6.           element.innerHTML = element.value + val;
  7.      }
  8. }
  9.  
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

Expand|Select|Wrap|Line Numbers
  1. function updateHours("['el1','el2','el3']", "['val1','val2','val3']")
  2. {
  3. }
  4.  
would update 3 elements taking into account the appropriate 3 values and

Expand|Select|Wrap|Line Numbers
  1. function updateHours("['el1','el2','el3']", "val")
  2. {
  3. }
  4.  
would update all 3 elements taking into account only 1 value.


Expand|Select|Wrap|Line Numbers
  1. function updateHours("el", "['val1','val2','val3']")
  2. {
  3. }
  4.  
would update 1 element with an assortment of values
Jan 8 '08 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
Pass the array as ['el1','el2','el3'] and use a for loop to loop over the elements, e.g.
Expand|Select|Wrap|Line Numbers
  1. for (i=0; i<el.length; i++) {
  2.      var element = document.getElementById(el[i]);
  3.      element.value = element.value + val;
  4. }
Jan 9 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.