Solved

Update contents of a <tr> element using innerHTML

Posted on 2006-06-24
2
1,198 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
ID: 16978936
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
ID: 16980416
Aaaah perfect! Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SharePoint 2013 Blog Template 2 37
JS Plugin 4 29
ngStyle with an input variable doesn't work 14 30
Refresh div via ajax without lost ccs <li> styles 8 3
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

867 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

15 Experts available now in Live!

Get 1:1 Help Now