473,573 Members | 3,961 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.create Element('ul');
ul.className = 'photoThumbnail ';

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

li = document.create Element('li');

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

img = document.create Element('img');
img.setAttribut e('src', path);

a.appendChild(i mg);
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 30614
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.create Element('ul');
ul.className = 'photoThumbnail ';

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

li = document.create Element('li');

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

img = document.create Element('img');
img.setAttribut e('src', path);

a.appendChild(i mg);
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.comwrot e:
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.create Element('ul');
ul.className = 'photoThumbnail ';

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

li = document.create Element('li');

a = document.create Element('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
2163
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 goes. Part I. Consider: A B C D M E F G H N O
3
1233
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
12493
by: Jay | last post by:
I have javascript that accepts 2 parameters like so... function showmsgbox(chkB, somestring) { xState=chkB.checked; if(xState) { chkB.parentElement.parentElement.style.backgroundColor='yellow'; } else
5
2275
by: Michel | last post by:
Hi Group of helpers, Following snippet of code: <HTML> <HEAD> <script> function color(value) { alert(value);
10
2495
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++) { items = "item" + (i + 1);
1
3757
by: quickcur | last post by:
Hi, here is my code: <html> <head> <script type="text/javascript"> function setImage(num) { alert(num); }
4
2565
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 the onclick function alltogether: the flow never gets into it, as if it hadn't been registered properly. Yet, an inspection of the DOM with the...
1
5038
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 can use the window.event to access the event once the function is called, but in Firefox it needs to be passed as a parameter.) In normal html...
8
4435
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 with the following code: var addCell = addRow.insertCell(1); addCell.id = cnt; addCell.colSpan = 3; var element =...
0
7792
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8218
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6435
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5605
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3738
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3744
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1330
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1049
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.