?
Solved

passing an Array in javascript

Posted on 2009-05-12
5
Medium Priority
?
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

770 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