setting outerHTML - can this be done?

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

    #1

    setting outerHTML - can this be done?

    Using IE 5.5 (sp2) - no other

    I have a table that need's to be sorted by single column using javascript.

    I have the code to do this but because of the attributes associated with the
    <tr> element, when the sorted values are copied back in to the table I want
    to replace the entire row not just the inner values!

    Any ideas why this doesn't work?

    var x = getElementByID( 'myTable'); //
    def get's object successfully
    alert(x.rows(0) .outerHTML);
    // before, shows current content
    x.rows(0).outer HTML = "<tr><td>pl ease work</td></tr>"; // Just to test,
    but goes no further, no error just bombs out!
    alert(x.rows(0) .outerHTML);
    // after

    thanks

    harry


  • harry

    #2
    Re: setting outerHTML - can this be done?

    sorry didn't realise what mess it would look with comments -

    var x = getElementByID( 'myTable');
    alert(x.rows(0) .outerHTML);
    x.rows(0).outer HTML = "<tr><td>pl ease work</td></tr>";
    alert(x.rows(0) .outerHTML); // never gets here


    Comment

    • Matt Kruse

      #3
      Re: setting outerHTML - can this be done?

      harry wrote:[color=blue]
      > Any ideas why this doesn't work?[/color]

      IE doesn't let you set the innerhtml or outerhtml of td's or tr's. You also
      can't set the innerhtml of a table. You can only set the outerhtml of the
      full table itself.

      If you want to manipulate table rows or cells, use DOM methods instead.
      Or replace the entire table's outerhtml, which could be very slow.

      --
      Matt Kruse



      Comment

      • VK

        #4
        Re: setting outerHTML - can this be done?

        The Table Object Model cannot be manipulated in such way.

        var myTable = getElementById( 'myTable');
        var myRow = myTable.insertR ow();
        if (myRow != null) {
        var myCell = myRow.insertCel l();
        myCell.innerHTM L = '<b>It works !!!!!!!!</b>';
        }
        else {
        window.alert('Y ou can not change a data-bound table. You have to change the
        data source itself');
        }


        More of useful reading:
        http://msdn.microsoft.com/workshop/a...uildtables.asp



        Comment

        • RobG

          #5
          Re: setting outerHTML - can this be done?

          harry wrote:
          [color=blue]
          > sorry didn't realise what mess it would look with comments -
          >
          > var x = getElementByID( 'myTable');
          > alert(x.rows(0) .outerHTML);
          > x.rows(0).outer HTML = "<tr><td>pl ease work</td></tr>";
          > alert(x.rows(0) .outerHTML); // never gets here
          >
          >[/color]

          Try using the DOM, it looks a but unweildy but it works.
          You can replace individual TDs if you want, you can also use
          cloneNode to make a copy of an element that contains a
          complex set of other elements (much simpler than slabs of
          createElement or innerHTML statements).

          <script type="text/javascript">
          function repRow(oldTR) {
          var newTR = document.create Element('tr');
          var newTD = document.create Element('td');
          var newTX = document.create TextNode('Repla cement text')
          newTD.appendChi ld(newTX);
          newTR.appendChi ld(newTD);
          oldTR.parentNod e.replaceChild( newTR,oldTR);
          }
          </script>

          <table id="myTable" border="1">
          <tr><td>Row above</td></tr>
          <tr id="aRow"><td>H ere is the current content</td></tr>
          <tr><td>Row below</td></tr>
          </table>
          <form action="" name="aForm">
          <input type="button" value="click" onclick="
          repRow(document .getElementById ('aRow'));">
          </form>

          Cheers, Rob.

          Comment

          Working...