DOM javascript - Imbed javascript in dynamically added Row

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ravi Singh (UCSD)

    DOM javascript - Imbed javascript in dynamically added Row

    Hello all

    Attached is a simple HTML file that adds and delete rows. In the add
    row function I set an attribute "onClick" this triggers the
    testMessage() function. When I try this in Firefox it works just fine
    however on IE it just refuses to work.

    What is interseting is the ROW that already exists has a similar
    'onClick' event which works when the page is loaded, but subsequent
    "row" additions to the table to not work in IE.

    Much thanks for any helfull insights that you can give.

    -Ravi Singh.





    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

    <HTML><HEAD><TI TLE>Testing DOM insertions</TITLE>
    <META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
    <SCRIPT type=text/javascript>
    var info = new Array("TD 1: index 0", "TD 2: index 1", "TD 3: index
    2");
    function insertTR(){
    var newTR = document.create Element("tr");
    newTR.setAttrib ute('onclick', "testMessage(th is)");
    for (i=0;i<info.len gth; i++)
    {
    var newTD= document.create Element("TD");
    var newText = document.create TextNode(info[i]);
    newTD.appendChi ld(newText)
    newTR.appendChi ld(newTD);

    }
    var tbElm =
    document.getEle mentById("table 1").getElements ByTagName("TBOD Y")[0];
    tbElm.appendChi ld(newTR);
    // }

    }

    function deleteTR()
    {
    var tbl = document.getEle mentById('table 1');
    var lastRow = tbl.rows.length ;
    if (lastRow > 0)
    tbl.deleteRow(l astRow - 1);
    else
    alert("No more rows left to delete!");
    }





    function testMessage(oRo w) {

    alert(oRow);


    }
    </SCRIPT>

    <META content="MSHTML 6.00.2900.2627" name=GENERATOR> </HEAD>
    <BODY>


    <TABLE id=table1 border=1><TBODY >


    <TR onClick="testMe ssage(this)"><T D>TD 1: index 0</TD>
    <TD>TD 2: index 1</TD>
    <TD>TD 3: index 2</TD></TR>

    </TBODY></TABLE>
    <P><INPUT onclick=insertT R(); type=button value="Insert Row">
    <INPUT onclick=deleteT R(); type=button value="Delete Row"></P>

    </BODY></HTML>

  • Ravi Singh (UCSD)

    #2
    Re: DOM javascript - Imbed javascript in dynamically added Row

    Arrgh!! I got it

    use newTR.onclick = function(){test Message(this);} ;


    instead of
    newTR.setAttrib ute('onclick', "testMessage(th is)");

    now my question is "is setAttribute" a non preferred way of doing
    things?

    why?? does it work in firefox and not in IE

    Thanks

    Ravi Singh.


    Ravi Singh (UCSD) wrote:[color=blue]
    > Hello all
    >
    > Attached is a simple HTML file that adds and delete rows. In the add
    > row function I set an attribute "onClick" this triggers the
    > testMessage() function. When I try this in Firefox it works just fine
    > however on IE it just refuses to work.
    >
    > What is interseting is the ROW that already exists has a similar
    > 'onClick' event which works when the page is loaded, but subsequent
    > "row" additions to the table to not work in IE.
    >
    > Much thanks for any helfull insights that you can give.
    >
    > -Ravi Singh.
    >
    >
    >
    >
    >
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    >
    > <HTML><HEAD><TI TLE>Testing DOM insertions</TITLE>
    > <META http-equiv=Content-Type content="text/html;[/color]
    charset=iso-8859-1">[color=blue]
    > <SCRIPT type=text/javascript>
    > var info = new Array("TD 1: index 0", "TD 2: index 1", "TD 3: index
    > 2");
    > function insertTR(){
    > var newTR = document.create Element("tr");
    > newTR.setAttrib ute('onclick', "testMessage(th is)");
    > for (i=0;i<info.len gth; i++)
    > {
    > var newTD= document.create Element("TD");
    > var newText = document.create TextNode(info[i]);
    > newTD.appendChi ld(newText)
    > newTR.appendChi ld(newTD);
    >
    > }
    > var tbElm =
    > document.getEle mentById("table 1").getElements ByTagName("TBOD Y")[0];
    > tbElm.appendChi ld(newTR);
    > // }
    >
    > }
    >
    > function deleteTR()
    > {
    > var tbl = document.getEle mentById('table 1');
    > var lastRow = tbl.rows.length ;
    > if (lastRow > 0)
    > tbl.deleteRow(l astRow - 1);
    > else
    > alert("No more rows left to delete!");
    > }
    >
    >
    >
    >
    >
    > function testMessage(oRo w) {
    >
    > alert(oRow);
    >
    >
    > }
    > </SCRIPT>
    >
    > <META content="MSHTML 6.00.2900.2627" name=GENERATOR> </HEAD>
    > <BODY>
    >
    >
    > <TABLE id=table1 border=1><TBODY >
    >
    >
    > <TR onClick="testMe ssage(this)"><T D>TD 1: index 0</TD>
    > <TD>TD 2: index 1</TD>
    > <TD>TD 3: index 2</TD></TR>
    >
    > </TBODY></TABLE>
    > <P><INPUT onclick=insertT R(); type=button value="Insert Row">
    > <INPUT onclick=deleteT R(); type=button value="Delete Row"></P>
    >
    > </BODY></HTML>[/color]

    Comment

    • BootNic

      #3
      Re: DOM javascript - Imbed javascript in dynamically added Row

      > "Ravi Singh (UCSD)" <ravisingh11@gm ail.com> wrote:[color=blue]
      > news:1116957676 .051995.79770@g 49g2000cwa.goog legroups.com... .
      > var tbElm =
      > document.getEle mentById("table 1").getElements ByTagName("TBOD Y")[0];
      > tbElm.appendChi ld(newTR);[/color]


      var tbElm =
      document.getEle mentById("table 1").getElements ByTagName("TBOD Y")[0];
      tbElm.appendChi ld(newTR);
      document.body.i nnerHTML = document.body.i nnerHTML

      --
      BootNic Tuesday, May 24, 2005 4:02 PM

      All my humor is based upon destruction and despair. If the whole world was tranquil, without disease
      and violence, I'd be standing on the breadline right in back of J. Edgar Hoover.
      *Lenny Bruce US comedian, satirist, author*


      Comment

      • RobG

        #4
        Re: DOM javascript - Imbed javascript in dynamically added Row

        Ravi Singh (UCSD) wrote:[color=blue]
        > Arrgh!! I got it[/color]

        Please don't top-post and trim quotes to the relevant bits.
        [color=blue]
        >
        > use newTR.onclick = function(){test Message(this);} ;
        >
        >
        > instead of
        > newTR.setAttrib ute('onclick', "testMessage(th is)");
        >
        > now my question is "is setAttribute" a non preferred way of doing
        > things?
        >
        > why?? does it work in firefox and not in IE
        >[/color]

        Have a read of this thread:

        <URL:http://groups-beta.google.com/group/comp.lang.javas cript/browse_frm/thread/fcc6057bd636839 f/9e08810b1437075 5?tvc=1&q=setAt tribute+table+f unction&hl=en#9 e08810b14370755 >


        --
        Rob

        Comment

        • Richard Cornford

          #5
          Re: DOM javascript - Imbed javascript in dynamically added Row

          BootNic wrote:[color=blue][color=green]
          >> Ravi Singh (UCSD) wrote:
          >> var tbElm =
          >> document.getEle mentById("table 1").getElements ByTagName(
          >> "TBODY")[0];
          >> tbElm.appendChi ld(newTR);[/color]
          >
          >
          > var tbElm =
          > document.getEle mentById("table 1").getElements ByTagName(
          > "TBODY")[0];
          > tbElm.appendChi ld(newTR);
          > document.body.i nnerHTML = document.body.i nnerHTML[/color]

          You are proposing re-building almost the entire DOM each and every time
          an element is added, as an alternative to replacing;-

          newTR.setAttrib ute('onclick', "testMessage(th is)");

          - with:-

          newTR.onclick = function(){test Message(this);} ;

          -?

          Richard.


          Comment

          • Ravi Singh (UCSD)

            #6
            Re: DOM javascript - Imbed javascript in dynamically added Row

            Richard,

            With this call newTR.onclick = function(){test Message(this);} ­; --
            you build the entire DOM tree. But IE does not support setAttribute for
            imbedding javascript functionality?


            Thanks
            -Ravi.

            Comment

            • Ravi Singh (UCSD)

              #7
              Re: DOM javascript - Imbed javascript in dynamically added Row

              thanks for the pointer Rob, very helpful.

              Comment

              • Richard Cornford

                #8
                Re: DOM javascript - Imbed javascript in dynamically added Row

                Ravi Singh (UCSD) wrote:[color=blue]
                > With this call newTR.onclick = function(){test Message(this);} ­;
                > -- you build the entire DOM tree.[/color]

                What are you talking about?
                [color=blue]
                > But IE does not support setAttribute
                > for imbedding javascript functionality?[/color]

                The W3C setAttribute method adds an Attribute Node to an Element Node,
                even on IE. Side-effects depend on the implementation (and which type of
                DOM is being scripted).

                Richard.


                Comment

                • Random

                  #9
                  Re: DOM javascript - Imbed javascript in dynamically added Row

                  BootNic wrote:[color=blue][color=green]
                  > > "Ravi Singh (UCSD)" <ravisingh11@gm ail.com> wrote:
                  > > news:1116957676 .051995.79770@g 49g2000cwa.goog legroups.com... .
                  > > var tbElm =
                  > > document.getEle mentById("table 1").getElements ByTagName("TBOD Y")[0];
                  > > tbElm.appendChi ld(newTR);[/color]
                  >
                  >
                  > var tbElm =
                  > document.getEle mentById("table 1").getElements ByTagName("TBOD Y")[0];
                  > tbElm.appendChi ld(newTR);
                  > document.body.i nnerHTML = document.body.i nnerHTML
                  >
                  > --
                  > BootNic Tuesday, May 24, 2005 4:02 PM[/color]
                  ....

                  Why do the innerHTML = innerHTML? I've never seen that done before and
                  I'm curious as to what purpose it serves. Compatibility?

                  Comment

                  • Richard Cornford

                    #10
                    Re: DOM javascript - Imbed javascript in dynamically added Row

                    Random wrote:[color=blue]
                    > BootNic wrote:[/color]
                    <snip>[color=blue][color=green]
                    >> var tbElm =
                    >> document.getEle mentById("table 1").getElements ByTagName(
                    >> "TBODY")[0]; tbElm.appendChi ld(newTR);
                    >> document.body.i nnerHTML = document.body.i nnerHTML[/color][/color]
                    <snip>[color=blue]
                    > Why do the innerHTML = innerHTML? I've never seen that done
                    > before and I'm curious as to what purpose it serves.[/color]

                    IE only internally generates event handling functions from HTML
                    attributes when it is parsing HTML source code, so this 'trick' is to
                    serialise the DOM into HTML source code and then use that to replace the
                    existing DOM (or at least a big chink of it). The innerHTML property
                    parses its string 'argument' (argument to the internal 'setter') as HTML
                    and builds a DOM branch with it in the way IE does for external HTML, so
                    the internal intrinsic event handlers are created for attribute,
                    including those added with setAttribute (as they will be apparent in the
                    HTML resulting from serialising the DOM).
                    [color=blue]
                    > Compatibility?[/color]

                    Voodoo; it is one of those things that broadly works, in the same way
                    that most - eval - abuses broadly work, and is propagated for that
                    reason alone.

                    However, using this technique allows people to remain ignorant of the
                    numerous better alternatives offered in the DOM (including very
                    cross-browser approaches such as assigning a function reference to event
                    handling properties of element objects), which are more efficient
                    approaches by orders of magnitude as serialising and then re-parsing the
                    DOM in order to get back to where you started (mostly, as all
                    script-assigned handlers and expandos are potentially lost in the
                    process) is ridiculously over-the-top.

                    Richard.


                    Comment

                    Working...