PDA

View Full Version : sending array as function parameter



boogyman
01-08-2008, 04:29 PM
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?



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



function updateHours("['el1','el2','el3']", "['val1','val2','val3']")
{
}

would update 3 elements taking into account the appropriate 3 values and



function updateHours("['el1','el2','el3']", "val")
{
}

would update all 3 elements taking into account only 1 value.




function updateHours("el", "['val1','val2','val3']")
{
}

would update 1 element with an assortment of values

Twey
01-08-2008, 05:36 PM
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:
if(el != false && val != false)or, to remove redundancy,
if(el && val)I think you probably mean:
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.

boogyman
01-08-2008, 06:45 PM
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.



<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


<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??


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.

Twey
01-08-2008, 07:13 PM
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.