Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    determining onclick event of dynamic links...plz help(urgent)..

    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);

    }
    }

  • #2
    Regular Coder
    Join Date
    Jan 2006
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts

  • #3
    Regular Coder
    Join Date
    May 2004
    Location
    Alabama, USA
    Posts
    237
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi frnd2chat, Welcome to CodingForums!

    Here's a quick idea...
    Code:
    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:
    Code:
    var aa=document.createElement('a');
    aa.href=' http://localhost/index.html';
    aa.appendChild(textNode);
    cellLeft.appendChild(aa);
    aa.onclick = tblLinkOnClick;
    Last edited by MikeFoster; 02-26-2007 at 10:17 PM.

  • #4
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thnx Mike

    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?
    Last edited by frnd2chat; 02-26-2007 at 11:05 PM.

  • #5
    Regular Coder
    Join Date
    May 2004
    Location
    Alabama, USA
    Posts
    237
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are you returning false from tblLinkOnClick?
    Code:
    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.
    Code:
    <a href='' onclick='something'>
    Show us some of the html that has the links.

  • #6
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thnx mike...it worked...can i determine exactly which link has been clicked?
    Last edited by frnd2chat; 02-28-2007 at 08:01 PM.

  • #7
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    plz reply......

  • #8
    Regular Coder
    Join Date
    May 2004
    Location
    Alabama, USA
    Posts
    237
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi frnd2chat,

    Code:
    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
    }

  • #9
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...

  • #10
    Regular Coder
    Join Date
    May 2004
    Location
    Alabama, USA
    Posts
    237
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Add/remove class names on the TD objects. There are many threads here that show how to do that.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •