Solved

Update contents of a <tr> element using innerHTML

Posted on 2006-06-24
2
1,204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
disable open new tab with right mouse click in <a> tag 3 40
Check input text, Number 7 54
XML loaded in a form with dropdown 6 51
Angular2 remove duplicates 11 23
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

739 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