Solved

passing an Array in javascript

Posted on 2009-05-12
5
236 Views
Last Modified: 2012-05-06
Hi,

I am simply trying to pass an array from one javascript function to another when the tinyPlusSubrowImage is clicked.

The variables in the array are elements, but I would also like to pass strings at the same time in the array.

I am seeing the following error in firebug:

missing ] after element list
 test([object HTMLTableRowElement],[object HTMLTableElement])

I *think* I am passing by reference as I should be. Can anyone see what I am doing wrong ?

Thanks
P.S I notice that the plus signs are getting stripped out of my function first in the code box. I am putting two pluses, one to either side of array on the tinyPlusSubrowImage line

function second(array){
  alert(array);
} 
 
function first(outerRow, outerTable){
var array = new Array();
         array[0] = outerRow;
         array[1] = outerTable;
         tinyPlusSubrowImage.setAttribute('onclick','second(' + array + ')');
}

Open in new window

0
Comment
Question by:COMPSUPP
  • 2
  • 2
5 Comments
 
LVL 14

Expert Comment

by:sam2912
ID: 24362849
You cannot do that because setting the attribute is by a string. You cannot put an object into the onclick attribute. What you should do is to pass the ID of the objects.

If you need further help kindly post your page code here, or a link to it so we can take a look.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 24363106
Have you tried this:


tinyPlusSubrowImage.onclick = second(array);

Open in new window

0
 
LVL 42

Expert Comment

by:David S.
ID: 24369271
Badotz, that will call the function right way. The way to get around that is to put it inside an anonymous function:

P.S. It's best to avoid setting event handlers with setAttribute().
tinyPlusSubrowImage.onclick = function(){ second(array); };

Open in new window

0
 
LVL 29

Expert Comment

by:Badotz
ID: 24370011
Kravimir: Of course, you are right. I must have still been asleep when I posted that :=/

But shouldn't it really be:


tinyPlusSubrowImage.onclick = function(array){ second(array); };

Open in new window

0
 
LVL 42

Accepted Solution

by:
David S. earned 50 total points
ID: 24370058
Nope. The argument for the anonymous function won't be passed until the event is triggered, in which case the only variable to be passed to it (if any) would be the event object.

You could write it like the following code, but doing so is only needed when inside a loop. (I used two different variable names for clarity. In practice I'd use the same name.) This is called "closures", by the way.
tinyPlusSubrowImage.onclick = (function(myArray){
  function(){ second(myArray); };
})(array);

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question