Solved

Update contents of a <tr> element using innerHTML

Posted on 2006-06-24
2
1,197 Views
Last Modified: 2012-08-13
Hey guys,

I have a table I am generating, for each row I am currently also generating a form in another row with the same values. These two rows can be "toggled" between which means the "display" row is toggled off and the "edit" row is toggled on.

Generating a seperate form for each row was placing unnecessary load on the server so I wrote a function which generates the HTML source code for the "editing" form based on arguments passed to it, now the problem comes where I need to insert this generated code into the "edit" row.

I am aware of the problems associated with using innerHTML directly on <tr> elements in IE and had a solution which worked temporarily but now does not.

The javascript I am using is so:

      var tableEdit = document.getElementById("tableEdit" + id);
      try {
            tableEdit.innerHTML = tableEditHTML;
      }
      catch (e) {
              // IE fails unless we wrap the string in another element.
              var wrappingDiv = document.createElement('div');
            wrappingDiv.innerHTML = tableEditHTML;
              tableEdit.appendChild(wrappingDiv);
      }

This is being used on the following HTML:

      <table>
            <tr id="tableEdit'.$r['id'].'" class="toggleHide rowEdit">
            </tr>
      </table>

Note: The variable "tableEditHTML" contains <td> tags and when the function is called to populate the "tableEdit" row with HTML so is a function which switches the data display with the data editing rows.

The code works in Firefox but not in IE. I have verified that "tableEditHTML" is actually true.
0
Comment
Question by:shaggy_the_sheep
2 Comments
 
LVL 25

Accepted Solution

by:
devic earned 350 total points
Comment Utility
hi shaggy_the_sheep, you can not use tr.innerHTML, so your tableEditHTML should be without <td>

here is an example what can you do:
=============================
<script>
function setMyHTML(curId)
{
      var tableEditHTML = "<b>hello</b> world!"
      var row = document.getElementById("tableEdit" + curId);
      if(row)
      {
            while(row.cells.length>0)
            {
                  row.deleteCell(0);
            }
            
            var cell = row.insertCell(0);
            cell.innerHTML = tableEditHTML;
      }
}
</script>
 <table border=1>
          <tr id="tableEdit5" class="toggleHide rowEdit">
          </tr>
 </table>
<button onclick="setMyHTML(5)">setMyHTML</button>
0
 
LVL 5

Author Comment

by:shaggy_the_sheep
Comment Utility
Aaaah perfect! Thanks
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now