Invoke onclick-Event in JS

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andreas Knollmann

    #1

    Invoke onclick-Event in JS

    Hi,

    I create an object like this:
    var cell = document.create Element("td").
    It doesn't have to be cell.

    I want this cell to use the onclick event. What doesn't work in the IE as
    well as with Mozilla is:
    cell.onclick = "whatever() ";

    What works only with Mozilla is:
    cell.setAttribu te("onclick", "whatever() ");

    So my question: Is there any way to get this done in the IE?

    thanks,
    Andreas


  • DU

    #2
    Re: Invoke onclick-Event in JS

    Andreas Knollmann wrote:[color=blue]
    > Hi,
    >
    > I create an object like this:
    > var cell = document.create Element("td").
    > It doesn't have to be cell.
    >
    > I want this cell to use the onclick event. What doesn't work in the IE as
    > well as with Mozilla is:
    > cell.onclick = "whatever() ";
    >
    > What works only with Mozilla is:
    > cell.setAttribu te("onclick", "whatever() ");
    >[/color]

    It is not recommendable to use setAttribute method when there is a
    better way (or a direct way) to set an attribute.
    [color=blue]
    > So my question: Is there any way to get this done in the IE?
    >
    > thanks,
    > Andreas
    >
    >[/color]

    RefObjCell.oncl ick = new Function("evt", "function body here");

    e.g.:

    RefObjCell.oncl ick = new Function("evt", "alert('an onclick event just
    happened inside a table cell');");

    Tested and working in Opera 7.50 PR1 build 3494, MSIE 6 SP1, Mozilla 1.3+

    DU

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Invoke onclick-Event in JS

      "Andreas Knollmann" <cmsi@gmx.net > writes:
      [color=blue]
      > I create an object like this:
      > var cell = document.create Element("td").[/color]
      [color=blue]
      > I want this cell to use the onclick event. What doesn't work in the IE as
      > well as with Mozilla is:
      > cell.onclick = "whatever() ";[/color]

      No, the value assigned to the onclick attribute must be a function,
      not a string.
      [color=blue]
      > What works only with Mozilla is:
      > cell.setAttribu te("onclick", "whatever() ");
      >
      > So my question: Is there any way to get this done in the IE?[/color]


      cell.onclick = function(){what ever();}
      or even
      cell.onclick = whatever;

      Notice that most browsers sends the event object as an argument to the
      function (as sepcified by W3C DOM Events), while IE keeps it as a
      global variable. If your code uses the event object, you will probably
      write:
      cell.onclick = function(event) {event = event||window.e vent;
      // your code using event
      };

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      Working...