473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to pass a parameter using the dom in a.onclick = function() {...} event?

SM
Hello,
I have two functions: show_thumbnail() and show_photo(). See code
below.

In show_thumbnail() i create a <ullist using Javascript and the DOM.
In the tag <ai create a onclick function and im passing two
parameters to the show_photo() function, the path of the image and the
description

In show_photo(), the parameters are always the last image and
description of the array.
How come?

Thanks
Marco


function show_thumbnail(thumbnail)
{
...
var path, description;
var li, a, img;

var ul = document.createElement('ul');
ul.className = 'photoThumbnail';

for(var i=0; i<thumbnail.length; i++)
{
path = thumbnail[i].path;
description = thumbnail[i].description;

li = document.createElement('li');

a = document.createElement('a');
a.onclick = function() { show_photo(path, description); } ??????

img = document.createElement('img');
img.setAttribute('src', path);

a.appendChild(img);
li.appendChild(a);
ul.appendChild(li);
}
}

function show_photo(path, description)
{
alert(path); ???always showa the last path of the array
}

May 25 '07 #1
2 30601
SM wrote:
Hello,
I have two functions: show_thumbnail() and show_photo(). See code
below.

In show_thumbnail() i create a <ullist using Javascript and the DOM.
In the tag <ai create a onclick function and im passing two
parameters to the show_photo() function, the path of the image and the
description

In show_photo(), the parameters are always the last image and
description of the array.
How come?

Thanks
Marco


function show_thumbnail(thumbnail)
{
...
var path, description;
var li, a, img;

var ul = document.createElement('ul');
ul.className = 'photoThumbnail';

for(var i=0; i<thumbnail.length; i++)
{
path = thumbnail[i].path;
description = thumbnail[i].description;

li = document.createElement('li');

a = document.createElement('a');
a.onclick = function() { show_photo(path, description); } ??????

img = document.createElement('img');
img.setAttribute('src', path);

a.appendChild(img);
li.appendChild(a);
ul.appendChild(li);
}
}

function show_photo(path, description)
{
alert(path); ???always showa the last path of the array
}
Because there's only one variable called "path", and all the instances of Function which you create and use as onclick handlers use *it*.

Once you've completed the loop, its value will be the last value, so there you go.
May 27 '07 #2
On May 26, 8:57 am, SM <servandomont...@gmail.comwrote:
Hello,
I have two functions: show_thumbnail() and show_photo(). See code
below.

In show_thumbnail() i create a <ullist using Javascript and the DOM.
In the tag <ai create a onclick function and im passing two
parameters to the show_photo() function, the path of the image and the
description

In show_photo(), the parameters are always the last image and
description of the array.
How come?
Because the anonymous functions that you assign all have closures back
to the same instaces of path and description.

Thanks
Marco

function show_thumbnail(thumbnail)
{
...
var path, description;
var li, a, img;

var ul = document.createElement('ul');
ul.className = 'photoThumbnail';

for(var i=0; i<thumbnail.length; i++)
{
path = thumbnail[i].path;
description = thumbnail[i].description;

li = document.createElement('li');

a = document.createElement('a');
a.onclick = function() { show_photo(path, description); } ??????
There are a few solutions, here's two:

a.onclick = new Function(
'show_photo(' + path + ',' + description + ')'
)

or

a.onclick = (function(p, d) {
return function(){ show_photo(p, d) }
})(path, description);

or you could pass path and description to some other function that
returns the new function. There is a good post here from Mike Winter:

<URL:
http://groups.google.com.au/group/co...9e4b4d3d8d3144
>

--
Rob

May 27 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dark Magician | last post by:
Comrades: Am trying to build a UI widget. I'm sure part of the problem is proper variable scope or object reference, and part of the problem may be the way I'm calling the function, but, here...
3
by: manmit.walia | last post by:
Hi, I have a small question...Is there a way to do a postback to the same page and pass a value to a function OnClick? Example: void GetCityName(string c) { string city = c; }
3
by: Jay | last post by:
I have javascript that accepts 2 parameters like so... function showmsgbox(chkB, somestring) { xState=chkB.checked; if(xState) {...
5
by: Michel | last post by:
Hi Group of helpers, Following snippet of code: <HTML> <HEAD> <script> function color(value) { alert(value);
10
by: Sean Dockery | last post by:
I have the following HTML file that I've been using for testing... <html> <head> <script type="text/javascript"> <!-- function handleWindowLoad() { var items = ; for (var i = 0; i < 11; i++)...
1
by: quickcur | last post by:
Hi, here is my code: <html> <head> <script type="text/javascript"> function setImage(num) { alert(num); }
4
by: manu3d | last post by:
Hi everybody, I'm generating some DIVs dinamically, inclusive of an event function to run when the user clicks on them. While the following lines work perfectly in FF 1.5, I.E. 6.0 ignores...
1
by: Ron | last post by:
Hi, I'm using javascript to dynamically generate a table (ajax/xml). What I want to achieve is when a table cell is clicked it needs to call a function and pass the event as a parameter (In IE I...
8
by: phub11 | last post by:
Hi all, I have placed a button in a cell, and I'm trying to use the "onclick" event to pass the ID of the cell (that contains the button) to another function. Could someone tell me what's wrong...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.