how to implement onlick event on a node?

C

Cylix

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;
node.onclick=alert(aryFiles);
imgNode=getFileTypeNode(getFileExt(aryFiles));
txtNode=document.createTextNode(aryFiles)
node.appendChild(imgNode);
node.appendChild(txtNode);
d.appendChild(node);
}
}

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

Ian Collins

Cylix said:
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;
node.onclick=alert(aryFiles);

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

You were calling alert.
 
M

Moses

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

Cylix said:
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;
node.onclick=alert(aryFiles);


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

You were calling alert.
 
I

Ian Collins

Moses said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top