add onclick event using setAttribute

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

    add onclick event using setAttribute

    All,

    I was trying to add an onclick event using setAttribute to a <tdfrom
    javascript. Apparently the code doesnot seem to work in FF3 but works
    pretty well in IE7.
    Here is the snippet for ur reference:
    <...
    var _table=document .createElement( "table");
    _tbody=document .createElement( "tbody");

    var _tr = document.create Element("tr");
    var _td = document.create Element("td");
    _td.innerHTML=" 6";
    _td.id=""+x+"_" +y+"";
    _td.setAttribut e("onclick",fun ction(){check(e vent);});
    _tr.appendChild (_td);
    _tbody.appendCh ild(_tr);
    _table.appendCh ild(_tbody");
    ....
    >
    And here is function "Check's" definition
    function check(e){
    if (!e)
    {
    var e=window.event;
    alert(e+" :None");
    }
    if (e.target)
    {

    targ=e.target;
    alert("FF/Netscape: ");
    }
    else if (e.srcElement)
    {
    targ=e.srcEleme nt;
    alert("IE");
    document.getEle mentById(e.srcE lement.id).inne rHTML="0";
    }
    alert(targ.tagN ame);
    }

    Any help in tracking this problem would be really useful.
  • Jorge

    #2
    Re: setAttribute

    On Jul 19, 1:49 pm, Sri02 <sri.ram...@gma il.comwrote:
    >
    _td.setAttribut e("onclick",fun ction(){check(e vent);});
    >
    _td.onclick= check;

    --Jorge.

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: setAttribute

      Sri02 wrote:
      I was trying to add an onclick event using setAttribute to a <tdfrom
      javascript. Apparently the code doesnot seem to work in FF3 but works
      pretty well in IE7.
      setAttribute() has a number of buggy implementations , avoid it.
      Here is the snippet for ur reference:
      ^^
      This is not an Internet chat.
      <...
      var _table=document .createElement( "table");
      _tbody=document .createElement( "tbody");
      >
      var _tr = document.create Element("tr");
      var _td = document.create Element("td");
      _td.innerHTML=" 6";
      Using the proprietary innerHTML property is not necessary for setting
      the content of an element to "6". Especially not when you are using
      standards-compliant DOM methods already.

      _td.appendChild (document.creat eTextNode("6")) ;
      _td.id=""+x+"_" +y+"";
      The leading and trailing concatenation is inefficient and superfluous.

      _td.id = x + "_" + y;
      _td.setAttribut e("onclick",fun ction(){check(e vent);});
      As Jorge said.
      _tr.appendChild (_td);
      _tbody.appendCh ild(_tr);
      _table.appendCh ild(_tbody");
      There is an extra <"character.
      ...
      All of this should be secured by a runtime feature test before use, of course:

      if (_td && isMethod(_td, "appendChil d"))
      {
      // ...
      }
      And here is function "Check's" definition
      function check(e){
      if (!e)
      {
      var e=window.event;
      alert(e+" :None");
      }
      if (e.target)
      This should be at least

      if (e)
      {
      var t = e.target || e.srcElement;
      if (t)
      {
      // ...
      }
      }

      No need to have a separate branch for each DOM.


      HTH

      PointedEars
      --
      Anyone who slaps a 'this page is best viewed with Browser X' label on
      a Web page appears to be yearning for the bad old days, before the Web,
      when you had very little chance of reading a document written on another
      computer, another word processor, or another network. -- Tim Berners-Lee

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: setAttribute

        Thomas 'PointedEars' Lahn wrote:
        Sri02 wrote:
        >_td.setAttribu te("onclick",fu nction(){check( event);});
        >
        As Jorge said.
        JFTR, the standards-compliant approach is:

        _td.addEventLis tener("click", function(e) { check(e); }, false);

        or

        _td.addEventLis tener(
        "click", {handleEvent: function(e) { check(e); }}, false);

        But MSHTML does not support it and attachEvent() is not a viable alternative.


        PointedEars
        --
        var bugRiddenCrashP ronePieceOfJunk = (
        navigator.userA gent.indexOf('M SIE 5') != -1
        && navigator.userA gent.indexOf('M ac') != -1
        ) // Plone, register_functi on.js:16

        Comment

        • Aaron Gray

          #5
          Re: setAttribute

          "Thomas 'PointedEars' Lahn" <PointedEars@we b.dewrote in message
          news:4881E450.4 080806@PointedE ars.de...
          Sri02 wrote:
          All of this should be secured by a runtime feature test before use, of
          course:
          >
          if (_td && isMethod(_td, "appendChil d"))
          {
          // ...
          }
          >
          Thomas,

          isMethod ? Where does "isMethod() " come from ? Surely this is not penned by
          you ?

          Sri02,
          _td.setAttribut e("onclick",fun ction(){check(e vent);});
          As for setting event handlers for both standards compliant and legacy
          browser support see my "addEvent - The late entry :)" thread :-



          Aaron

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: setAttribute

            Aaron Gray wrote:
            "Thomas 'PointedEars' Lahn" wrote:
            >Sri02 wrote:
            >All of this should be secured by a runtime feature test before use, of
            >course:
            >>
            > if (_td && isMethod(_td, "appendChil d"))
            > {
            > // ...
            > }
            >
            Thomas,
            >
            isMethod ? Where does "isMethod() " come from ? Surely this is not penned by
            you ?
            You may use any `isMethod' implementation you like as long as its license
            allows it, including mine.
            >_td.setAttribu te("onclick",fu nction(){check( event);});
            >
            As for setting event handlers for both standards compliant and legacy
            browser support see my "addEvent - The late entry :)" thread :-
            >
            http://groups.google.com/group/comp....45c0d89caf0a1#
            The approach you present there is old news and flawed, as is attachEvent().
            I was going to reply to that in more detail later, although this has been
            discussed to death here and elsewhere already.


            PointedEars
            --
            Prototype.js was written by people who don't know javascript for people
            who don't know javascript. People who don't know javascript are not
            the best source of advice on designing systems that use javascript.
            -- Richard Cornford, cljs, <f806at$ail$1$8 300dec7@news.de mon.co.uk>

            Comment

            • Aaron Gray

              #7
              Re: setAttribute

              "Thomas 'PointedEars' Lahn" <PointedEars@we b.dewrote in message
              news:4882881C.8 040504@PointedE ars.de...
              Aaron Gray wrote:
              >"Thomas 'PointedEars' Lahn" wrote:
              >>Sri02 wrote:
              >>All of this should be secured by a runtime feature test before use, of
              >>course:
              >>>
              >> if (_td && isMethod(_td, "appendChil d"))
              >> {
              >> // ...
              >> }
              >>
              >Thomas,
              >>
              >isMethod ? Where does "isMethod() " come from ? Surely this is not penned
              >by
              >you ?
              >
              You may use any `isMethod' implementation you like as long as its license
              allows it, including mine.
              Okay, you should really mention things like that though.
              >>_td.setAttrib ute("onclick",f unction(){check (event);});
              >>
              >As for setting event handlers for both standards compliant and legacy
              >browser support see my "addEvent - The late entry :)" thread :-
              >>
              >>
              >http://groups.google.com/group/comp....45c0d89caf0a1#
              >
              The approach you present there is old news and flawed, as is
              attachEvent().
              I was going to reply to that in more detail later, although this has been
              discussed to death here and elsewhere already.
              Great Thomas, I shall look forward to that then.

              Aaron

              Comment

              • Aaron Gray

                #8
                Re: setAttribute

                "Thomas 'PointedEars' Lahn" <PointedEars@we b.dewrote in message
                news:4882881C.8 040504@PointedE ars.de...
                Prototype.js was written by people who don't know javascript for people
                who don't know javascript. People who don't know javascript are not
                the best source of advice on designing systems that use javascript.
                -- Richard Cornford, cljs, <f806at$ail$1$8 300dec7@news.de mon.co.uk>
                Thomas,

                You seem to have made it impossible to find the original quoted source.

                Could you please provide a proper link from a news archive as demon no
                longer store this message and you having quoted it all over the place has
                made it impossible to find on Google.

                Thanks,

                Aaron


                Comment

                • Jorge

                  #9
                  Re: setAttribute

                  On Jul 20, 4:05 am, "Aaron Gray" <ang.use...@gma il.comwrote:
                  >
                  Could you please provide a proper link from a news archive as demon no
                  longer store this message and you having quoted it all over the place has
                  made it impossible to find on Google.
                  >
                  This message



                  points to this thread :



                  --Jorge.

                  Comment

                  • Thomas 'PointedEars' Lahn

                    #10
                    Re: setAttribute

                    Aaron Gray wrote:
                    "Thomas 'PointedEars' Lahn" <PointedEars@we b.dewrote:
                    >Aaron Gray wrote:
                    >>"Thomas 'PointedEars' Lahn" wrote:
                    >>>Sri02 wrote:
                    >>>All of this should be secured by a runtime feature test before use, of
                    >>>course:
                    >>>>
                    >>> if (_td && isMethod(_td, "appendChil d"))
                    >>> {
                    >>> // ...
                    >>> }
                    >>Thomas,
                    Don't.
                    >>isMethod ? Where does "isMethod() " come from ? Surely this is not penned
                    >>by you ?
                    >You may use any `isMethod' implementation you like as long as its license
                    >allows it, including mine.
                    >
                    Okay, you should really mention things like that though.
                    Subscribers of this newsgroup are supposed to be intelligent beings who are
                    capable of typing "isMethod" into the text input of their favorite search
                    engine when they get a runtime error on calling isMethod().


                    PointedEars
                    --
                    var bugRiddenCrashP ronePieceOfJunk = (
                    navigator.userA gent.indexOf('M SIE 5') != -1
                    && navigator.userA gent.indexOf('M ac') != -1
                    ) // Plone, register_functi on.js:16

                    Comment

                    • Thomas 'PointedEars' Lahn

                      #11
                      Re: setAttribute

                      Aaron Gray wrote:
                      "Thomas 'PointedEars' Lahn" <PointedEars@we b.dewrote:
                      >Prototype.js was written by people who don't know javascript for people
                      >who don't know javascript. People who don't know javascript are not
                      >the best source of advice on designing systems that use javascript.
                      > -- Richard Cornford, cljs, <f806at$ail$1$8 300dec7@news.de mon.co.uk>
                      >
                      Thomas,
                      Don't.
                      You seem to have made it impossible to find the original quoted source.
                      Yes, it only seems to be so.
                      Could you please provide a proper link from a news archive as demon no
                      longer store this message and you having quoted it all over the place has
                      made it impossible to find on Google.
                      Having yourself evaluate the following as an ECMAScript Program will help:

                      if (me.googlodyte)
                      {
                      with (me.brainSimula tion)
                      {
                      if (navigator.go(
                      "http://groups.google.c om/advanced_search ",
                      "http://groups.google.c om/",
                      i18n("Advanced Groups Search")))
                      {
                      if (navigator.past e(
                      navigator.copy( perception.curr entPosting.body
                      .getNextEmailAd dr()),
                      i18n("Lookup the message with message ID")))
                      {
                      if (navigator.clic k(i18n("Lookup Message")))
                      {
                      while (navigator.read yState != 4);
                      navigator.read( );
                      }
                      }
                      }
                      }

                      while (!me.hasMinimum Clue())
                      {
                      if (me.getInformed About("Usenet") )
                      {
                      me.getLocalNews ReaderApp();
                      }
                      }

                      me.googlodyte = false;
                      }
                      else
                      {
                      me.lookupMsgId( me.brain.percep tion.currentPos ting.body.getNe xtMsgId());
                      }

                      Probably someone has told you this before.


                      PointedEars
                      --
                      Use any version of Microsoft Frontpage to create your site.
                      (This won't prevent people from viewing your source, but no one
                      will want to steal it.)
                      -- from <http://www.vortex-webdesign.com/help/hidesource.htm>

                      Comment

                      • Aaron Gray

                        #12
                        Re: setAttribute

                        "Jorge" <jorge@jorgecha morro.comwrote in message
                        news:01777676-43bd-4cfa-b1b1-0de443ba8623@34 g2000hsh.google groups.com...
                        On Jul 20, 4:05 am, "Aaron Gray" <ang.use...@gma il.comwrote:
                        >
                        Could you please provide a proper link from a news archive as demon no
                        longer store this message and you having quoted it all over the place has
                        made it impossible to find on Google.
                        >
                        This message



                        points to this thread :



                        Thanks,

                        Aaron


                        Comment

                        • Aaron Gray

                          #13
                          Re: setAttribute

                          "Thomas 'PointedEars' Lahn" <PointedEars@we b.dewrote in message
                          news:48830C82.2 010403@PointedE ars.de...
                          Aaron Gray wrote:
                          >"Thomas 'PointedEars' Lahn" <PointedEars@we b.dewrote:
                          >>Prototype.j s was written by people who don't know javascript for people
                          >>who don't know javascript. People who don't know javascript are not
                          >>the best source of advice on designing systems that use javascript.
                          >> -- Richard Cornford, cljs, <f806at$ail$1$8 300dec7@news.de mon.co.uk>
                          >>
                          >Thomas,
                          >
                          Don't.
                          >
                          >You seem to have made it impossible to find the original quoted source.
                          >
                          Yes, it only seems to be so.
                          >
                          >Could you please provide a proper link from a news archive as demon no
                          >longer store this message and you having quoted it all over the place has
                          >made it impossible to find on Google.
                          >
                          Having yourself evaluate the following as an ECMAScript Program will help:
                          >
                          if (me.googlodyte)
                          {
                          with (me.brainSimula tion)
                          {
                          if (navigator.go(
                          "http://groups.google.c om/advanced_search ",
                          "http://groups.google.c om/",
                          i18n("Advanced Groups Search")))
                          {
                          if (navigator.past e(
                          navigator.copy( perception.curr entPosting.body
                          .getNextEmailAd dr()),
                          i18n("Lookup the message with message ID")))
                          {
                          if (navigator.clic k(i18n("Lookup Message")))
                          {
                          while (navigator.read yState != 4);
                          navigator.read( );
                          }
                          }
                          }
                          }
                          >
                          while (!me.hasMinimum Clue())
                          {
                          if (me.getInformed About("Usenet") )
                          {
                          me.getLocalNews ReaderApp();
                          }
                          }
                          >
                          me.googlodyte = false;
                          }
                          else
                          {
                          me.lookupMsgId( me.brain.percep tion.currentPos ting.body.getNe xtMsgId());
                          }
                          >
                          Probably someone has told you this before.
                          >
                          >
                          PointedEars
                          --
                          Use any version of Microsoft Frontpage to create your site.
                          (This won't prevent people from viewing your source, but no one
                          will want to steal it.)
                          -- from <http://www.vortex-webdesign.com/help/hidesource.htm>
                          Have you not been told not to use the 'with' clause ?

                          Aaron


                          Comment

                          • Thomas 'PointedEars' Lahn

                            #14
                            Re: setAttribute

                            Aaron Gray wrote:
                            Have you not been told not to use the 'with' clause ?
                            Have you not been told to quote properly?


                            Score adjusted

                            PointedEars
                            --
                            var bugRiddenCrashP ronePieceOfJunk = (
                            navigator.userA gent.indexOf('M SIE 5') != -1
                            && navigator.userA gent.indexOf('M ac') != -1
                            ) // Plone, register_functi on.js:16

                            Comment

                            • RobG

                              #15
                              Re: setAttribute

                              On Jul 20, 10:24 am, "Aaron Gray" <ang.use...@gma il.comwrote:
                              "Thomas 'PointedEars' Lahn" <PointedE...@we b.dewrote in messagenews:488 1E450.4080806@P ointedEars.de.. .
                              >
                              Sri02 wrote:
                              All of this should be secured by a runtime feature test before use, of
                              course:
                              >
                               if (_td && isMethod(_td, "appendChil d"))
                               {
                                 // ...
                               }
                              >
                              Thomas,
                              >
                              isMethod ? Where does "isMethod() " come from ? Surely this is not penned by
                              you ?
                              He posted a version in this thread:

                              <URL:
                              http://groups.google.com.au/group/co...2963d7ec6dbc8b
                              >

                              A modified version of that is:

                              var isMethod = (function() {
                              var re = /\bfunction|obje ct\b/;
                              return function(o) {
                              return re.test(typeof o);
                              }
                              })();


                              to me that is only marginally more effective than:

                              if (obj) obj();

                              or

                              obj && obj();


                              which at least saves looking up the isMethod function to see what it
                              does.

                              The definition of a function in javscript is an object that implements
                              the internal [[Call]] method[1], however there is no definitive test
                              for that, the closest is to actually call it within a try..catch block
                              and see what eventuates, though that can also be error-prone and
                              likely far more effort than it's worth in the vast majority of cases.

                              Using typeof should be sufficient for native functions, however host
                              functions do not have to return 'function' and don't in at least one
                              popular browser.

                              You might like this thread if you have an hour or so spare - there's a
                              lot to read! :-)

                              <URL:
                              http://groups.google.com.au/group/co...7385810dfcdf9a
                              >

                              1. That isn't explicitly stated in ECMA-262 as far as I know, but can
                              be reasonably inferrred from sections 10.1.3 and 11.4.3


                              --
                              Rob

                              Comment

                              Working...