Results 1 to 4 of 4

Thread: sending array as function parameter

  1. #1
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default sending array as 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

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    element.innerHTML = element.value + val;
    Don't use innerHTML. If it has a value (I'm presuming this is a textarea) then what you mean to say is element.value += val;
    function updateHours("['el1','el2','el3']", "['val1','val2','val3']")
    Um... what? Are you trying to call it here or define it? If the former, you don't want the function keyword; if the latter, those are both invalid identifiers for arguments. Also, where's the array? Those are two strings.
    if(el!=null && val!=null)
    They'll never be null. They may be undefined, which will coerce to null (as you're using != rather than !==), but why not just check for undefined? You could just as well as this be doing:
    Code:
    if(el != false && val != false)
    or, to remove redundancy,
    Code:
    if(el && val)
    I think you probably mean:
    Code:
    function updateHours(els, vals) {
      if(!els || !vals)
        return;
    
      if(!(els instanceof Array))
        els = [els];
      if(!(vals instanceof Array))
        vals = [vals];
    
      for(var i = els.length - 1; i >= 0; --i)
        (typeof els[i] === "string" ? document.getElementById(els[i]) : els[i]).value += vals.join("");
    }
    ... but I can't be too sure.
    Last edited by Twey; 01-08-2008 at 04:42 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    element.innerHTML = element.value + val;
    Don't use innerHTML. If it has a value (I'm presuming this is a textarea) then what you mean to say is element.value += val;
    In this specific function I am using it to update the total hours that have been spent on a specific project. The user can input some float. This is only meant to be a reference since it is client-side... The field actually being updated is contained within a span tag, which is why I decided to use the innerHTML method.

    Code:
    <span id="tot_cat_hours">0</span>
    There are multiple "categories" of which a user can enter in data, so that is where the array comes in... In addition to each category, there is a total at the bottom that will be updated too.

    function updateHours("['el1','el2','el3']", "['val1','val2','val3']")
    Um... what? Are you trying to call it here or define it? If the former, you don't want the function keyword; if the latter, those are both invalid identifiers for arguments. Also, where's the array? Those are two strings.
    the actual update input field looks like
    Code:
    <input type="text" name="cat_hours" value="" onchange="updateHours(array, this.value); return false"> hours
    array is the 2 fields that need to be updated... given as an array parameter, but I dont know how to assign the array to the parameter? would it just be??
    Code:
    updateHours(['el1','el2'], this.value)
    el1 first element that would get updated (category)
    el2 second element to get updated (total)

    while this specific need is to just have this one value update the multiple elements, however I wanted to make it robust so that later if multiple values are needed that can be achieved... for instance... rather then taking just this specific value... take the value of every project on the page which would allow for the user to go back into any project without having to refresh the page, which is also another check that is performed... checked for negative values and the cumulative total cannot be greater than 168... eg work every second of every day for an entire week.
    Last edited by boogyman; 01-08-2008 at 05:55 PM. Reason: added colors

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The field actually being updated is contained within a span tag, which is why I decided to use the innerHTML method.
    <span>s don't have value attributes.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •