Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Adding a link to a table built in Javascript

Adding a link to a table built in Javascript

Scheduled Pinned Locked Moved JavaScript
javascripthelptutorialquestion
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    offroaderdan
    wrote on last edited by
    #1

    Hi all, This is hopefully a very simple question however I do not know the answer to it. I am trying to link a cell from a table created in Javascript to another function.

    var cell_1 = document.createElement("td");
    cell_1.style.textAlign = "center";
    cell_1.appendChild(document.createTextNode("Hi"));
    onclick = myFunction();

    This code ALL works apart from the onclick function, all I wanted to do is when the cell is clicked for example an alert message pops up. Hopefully that makes sense and if anybody can help that would be helpful :)

    enhzflepE B S T 4 Replies Last reply
    0
    • O offroaderdan

      Hi all, This is hopefully a very simple question however I do not know the answer to it. I am trying to link a cell from a table created in Javascript to another function.

      var cell_1 = document.createElement("td");
      cell_1.style.textAlign = "center";
      cell_1.appendChild(document.createTextNode("Hi"));
      onclick = myFunction();

      This code ALL works apart from the onclick function, all I wanted to do is when the cell is clicked for example an alert message pops up. Hopefully that makes sense and if anybody can help that would be helpful :)

      enhzflepE Offline
      enhzflepE Offline
      enhzflep
      wrote on last edited by
      #2

      Hi, you're basically there. Just 2 points:

      1. the onclick function belongs to cell_1
      2. You want to set the address of the function, not the result

      cell_1.onclick = myFunction;

      1 Reply Last reply
      0
      • O offroaderdan

        Hi all, This is hopefully a very simple question however I do not know the answer to it. I am trying to link a cell from a table created in Javascript to another function.

        var cell_1 = document.createElement("td");
        cell_1.style.textAlign = "center";
        cell_1.appendChild(document.createTextNode("Hi"));
        onclick = myFunction();

        This code ALL works apart from the onclick function, all I wanted to do is when the cell is clicked for example an alert message pops up. Hopefully that makes sense and if anybody can help that would be helpful :)

        B Offline
        B Offline
        BobJanova
        wrote on last edited by
        #3

        It might be better to create an a tag with a javascript: url:

        var cell_1 = document.createElement("td");
        cell_1.style.textAlign = "center";
        var link = document.createElement("a");
        link.appendChild(document.createTextNode("Hi"));
        link.href = 'javascript:myFunction()';

        That way, you get the usual link semantics (underlining, colour cues, cursor, tab order entry).

        1 Reply Last reply
        0
        • O offroaderdan

          Hi all, This is hopefully a very simple question however I do not know the answer to it. I am trying to link a cell from a table created in Javascript to another function.

          var cell_1 = document.createElement("td");
          cell_1.style.textAlign = "center";
          cell_1.appendChild(document.createTextNode("Hi"));
          onclick = myFunction();

          This code ALL works apart from the onclick function, all I wanted to do is when the cell is clicked for example an alert message pops up. Hopefully that makes sense and if anybody can help that would be helpful :)

          S Offline
          S Offline
          Sanjay K Gupta
          wrote on last edited by
          #4

          alternet way cell_1.onclick = new Function ('myFunction()');

          1 Reply Last reply
          0
          • O offroaderdan

            Hi all, This is hopefully a very simple question however I do not know the answer to it. I am trying to link a cell from a table created in Javascript to another function.

            var cell_1 = document.createElement("td");
            cell_1.style.textAlign = "center";
            cell_1.appendChild(document.createTextNode("Hi"));
            onclick = myFunction();

            This code ALL works apart from the onclick function, all I wanted to do is when the cell is clicked for example an alert message pops up. Hopefully that makes sense and if anybody can help that would be helpful :)

            T Offline
            T Offline
            twseitex
            wrote on last edited by
            #5

            to attach events see this // +++++ attach even function AtEv(X00,X01,X02,X03) // X00 pointer of object with event // X01 kind of event, string e.g. 'mouseover' // X02 pointer of event handler // e.g. keyDownTextField (e) // { // var keyCode = e.keyCode; // //... // } // X03 false bubbling event allowed // true bubbling event not allowed: Capturing of event thru handler // must be identical with avent deatch // returns true if event attched { var X04=false; // insert here checking of pointer and string ... if(X00.attachEvent) { X04=X00.attachEvent('on'+X01,X02); } else { if(X00.addEventListener) { X00.addEventListener(X01,X02,X03); X04=true; } } return X04; } // +++++ detach event function DatEv(X00,X01,X02,X03) // params see AtEv { var X04=false; // insert here checking of pointer and string ... if(X00.detachEvent) { X04=X00.detachEvent('on'+X01,X02); } else { if(X00.removeEventListener) { X00.removeEventListener(X01,X02,X03); X04=true; } } return X04; } example function EventHandler(e,X00) // e.g. X00 param // e placeholder for event { // do something with X00 and or e } // ----- create dynamic event handler var pointerOfDynamicEventHandler=new Function('e','EventHandler(e,' + here the value of X00 + ')'); // check pointer ... // ----- attach dynamic event handler var booleanFlag=AtEv(pointerofObjectWithEvent,'click',pointerOfDynamicEventHandler,false); // ------ don't forget to deatch event if you this not need

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups