...

View Full Version : determining onclick event of dynamic links...plz help(urgent)..



frnd2chat
02-26-2007, 08:22 PM
The code below displays a dynamic table with all its contents in the form of links.I wanna know as to how i can detect whether any of the links in the table has been clicked.As the table is dynamic i am not aware of the syntax of how to use the onClick evnt handler for these links.plz help


function disptable(mail)
{

var res=new Array();
res=mail.split("%");
var tbl = document.getElementById("results");
var lastRow = tbl.rows.length;

while(lastRow > 1)
{
tbl.deleteRow(lastRow - 1);
lastRow=lastRow-1;
}
for(i=0;i<res.length-1;i++)
{

lastRow=tbl.rows.length;
var iteration=res[i];
var row = tbl.insertRow(lastRow);
var cellLeft = row.insertCell(0);
var textNode = document.createTextNode(iteration);
var aa=document.createElement('a');
aa.href=' http://localhost/index.html';
aa.appendChild(textNode);
cellLeft.appendChild(aa);

}
}

JayStang
02-26-2007, 08:36 PM
this may help..

for firefox:
http://developer.mozilla.org/en/docs/DOM:element.addEventListener

for ie:
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/methods/attachevent.asp

MikeFoster
02-26-2007, 09:13 PM
Hi frnd2chat, Welcome to CodingForums!

Here's a quick idea...


function watchTblLinks(tblId)
{
var i, ar, tbl = document.getElementById(tblId);
ar = tbl.getElementsByTagName('a');
for (i = 0; i < ar.length; ++i) {
ar[i].onclick = tblLinkOnClick;
}
}

function tblLinkOnClick()
{
// one of the links in the table was clicked
// return true to allow navigation to this.href
// return false to not allow it
}


Note that watchTblLinks should be called from the onload listener, after the table is created.
Or, in your disptable function, add the green line:


var aa=document.createElement('a');
aa.href=' http://localhost/index.html';
aa.appendChild(textNode);
cellLeft.appendChild(aa);
aa.onclick = tblLinkOnClick;

frnd2chat
02-26-2007, 09:36 PM
It worked......u saved the day....one more thing.....m not able to turn the href to false...my page is gettin reloaded and m losing the previous content....how can i regain my content?

MikeFoster
02-27-2007, 01:46 AM
Are you returning false from tblLinkOnClick?


function tblLinkOnClick()
{
return false;
}


Do any of the links in the table have strings assigned to their onclick attribute? Note that this technique will over-write them.


<a href='' onclick='something'>


Show us some of the html that has the links.

frnd2chat
02-28-2007, 08:00 AM
thnx mike...it worked...can i determine exactly which link has been clicked?

frnd2chat
02-28-2007, 07:22 PM
plz reply......

MikeFoster
02-28-2007, 08:26 PM
Hi frnd2chat,



function tblLinkOnClick()
{
/*
"this" references the A element that was clicked.
So you have access to any of its properties.
For example:
*/
var linkHref = this.href;
var linkId = this.id;
var linkClass = this.className;
return false; // cancel default action
}

frnd2chat
03-03-2007, 03:21 PM
hi Mike...
how can i set CSS properties for the above table rows and links which are present in the table? i tried using classes but it only works for the table header as it is static and not for the dynamic rows and links...

MikeFoster
03-03-2007, 10:50 PM
Add/remove class names on the TD objects. There are many threads here that show how to do that.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum