Solved

passing an Array in javascript

Posted on 2009-05-12
5
233 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now