473,406 Members | 2,620 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,406 software developers and data experts.

how to implement onlick event on a node?

my function:
function displayFiles(files, curPath) {
var d = document.getElementById('folderContent');
d.innerHTML = '';
if (!(d&&files)) return;
var aryFiles = files.split('|');
var node, txtNode, imgNode, ext;
for (var i=0;i<aryFiles.length;i++) {
node = document.createElement('div');
node.className='file';
node.id=aryFiles[i];
node.onclick=alert(aryFiles[i]);
imgNode=getFileTypeNode(getFileExt(aryFiles[i]));
txtNode=document.createTextNode(aryFiles[i])
node.appendChild(imgNode);
node.appendChild(txtNode);
d.appendChild(node);
}
}

when the function run, the onlick event, ie alert(aryFiles[i]); is
fired.
but the event is never set. please let me know the right syntax.
Thanks a lot!

Apr 3 '07 #1
3 1849
Cylix wrote:
my function:
function displayFiles(files, curPath) {
var d = document.getElementById('folderContent');
d.innerHTML = '';
if (!(d&&files)) return;
var aryFiles = files.split('|');
var node, txtNode, imgNode, ext;
for (var i=0;i<aryFiles.length;i++) {
node = document.createElement('div');
node.className='file';
node.id=aryFiles[i];
node.onclick=alert(aryFiles[i]);
node.onclick=function(){ alert(aryFiles[i]); }

You were calling alert.

--
Ian Collins.
Apr 3 '07 #2
Hi Collins,

I have problem in the following code,

node.onclick=function(){ test(i);};

for (var i=0;i<5;i++)
{
bdy = document.getElementById('name');
node = document.createElement('div');
node.id=i;
node.onclick=function(){ test(i);};
newtext = document.createTextNode(i);
node.appendChild(newtext);
bdy.appendChild(node);
}
ie I am creating 5 divs and on the click event of the div I am
calling a function test(i),If I click the first div it should alert 1
and if I click the second it should alert 2 and so on....

The problem is all the divs alert 5, Only the last value of i is
alerted.


Thanks&Regards
Moses

On Apr 3, 7:02 am, Ian Collins <ian-n...@hotmail.comwrote:
Cylix wrote:
my function:
function displayFiles(files, curPath) {
var d = document.getElementById('folderContent');
d.innerHTML = '';
if (!(d&&files)) return;
var aryFiles = files.split('|');
var node, txtNode, imgNode, ext;
for (var i=0;i<aryFiles.length;i++) {
node = document.createElement('div');
node.className='file';
node.id=aryFiles[i];
node.onclick=alert(aryFiles[i]);

node.onclick=function(){ alert(aryFiles[i]); }

You were calling alert.

--
Ian Collins.

Apr 3 '07 #3
Moses wrote:
Hi Collins,
Please don't top post and my name's Ian!
I have problem in the following code,

node.onclick=function(){ test(i);};

for (var i=0;i<5;i++)
{
bdy = document.getElementById('name');
node = document.createElement('div');
node.id=i;
node.onclick=function(){ test(i);};
newtext = document.createTextNode(i);
node.appendChild(newtext);
bdy.appendChild(node);
}
ie I am creating 5 divs and on the click event of the div I am
calling a function test(i),If I click the first div it should alert 1
and if I click the second it should alert 2 and so on....

The problem is all the divs alert 5, Only the last value of i is
alerted.
That's because i is in the scope of displayFiles and the last value
assigned to it was 5. Don't use i, use the id of the element that was
clicked.

--
Ian Collins.
Apr 3 '07 #4

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

Similar topics

1
by: StvB | last post by:
Hi, I just started implementing adding a new node to an existing tree. In these statements, I add the node itself and attach the "end of editing" event handler to the event -------- id =...
10
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
4
by: DC Gringo | last post by:
I have a clickable image that I would like to convert into a server control imagebutton accessing a server-side onClick event and a client-side javascript onclick event as well. Is this possible? ...
5
by: Jeff Thies | last post by:
I have this IE specific bit of code for finding the originating node: var obj=window.event.srcElement; How do I do that cross browser (Opera, NS, Safari...)? Is there a standard DOM method? ...
0
by: Emerson | last post by:
The following assumes a System.Windows.Forms.DataGrid with a System.Data.DataTable set as the DataSource. I'm programming in C# Here's my scenario I click in a cell on a DataGrid. I enter some...
17
by: Spam Trap | last post by:
I am upgrading from VB6 where I had access to a treeview NodeClick event (see below)... but now VB.NET does not have this any more. Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)...
10
by: jack | last post by:
Hi guys, I am working on a project which requires an implementation of discrete event simulation in C using linked lists. I would greatly appreciate if someone could provide with some sources...
1
by: pooja | last post by:
i need to implement drag and drop in treeview in VB. Kindly help. My treeview contains activities maintained using XML Files. Hopefully, Thanks.
5
by: Klaudiusz Bryja | last post by:
Hi, This is for NetCF 2.0. I need to create event handling code which using reflection. I have some parameters in XML which describe how event should be handled. I have code to create...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.