Solved

Incrementing the innerHTML property like a string

Posted on 2006-06-21
2
367 Views
Last Modified: 2011-09-20
I'm writing a function that dynamically generates tables, by incrementing the innerHTML property of a surrounding <div>.

But, I need to break the procedure up into multiple lines, because of a few for loops.  But, when I try to increment .innerHTML using multiple statements, I loose the formatting on the table.

For example, I have:

parentElem.innerHTML = "<table id = '" + tableId + "' width = '99%' class = 'resulttext' style = 'margin: 4px 4px 6px 4px; color: #818284; background-color: #FFFFFF; border-collapse: collapse' border = '0' cellspacing = '0' cellpadding = '0'><tr>";


parentElem.innerHTML += "<td>Sample Text</td></tr></table>";

In this case, even though I formatted the table with specific styles, the formatting does not appear in the browser.  However, if I do this all on one line like:

parentElem.innerHTML = "<table id = '" + tableId + "' width = '99%' class = 'resulttext' style = 'margin: 4px 4px 6px 4px; color: #818284; background-color: #FFFFFF; border-collapse: collapse' border = '0' cellspacing = '0' cellpadding = '0'><tr><td>Hey hey!</td></tr></table>";

...then it works fine.  I don't understand why this is happening.  Can anyone explain what I'm doing wrong here?

0
Comment
Question by:chsalvia
[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 23

Accepted Solution

by:
Jens Fiederer earned 125 total points
ID: 16955063
Everytime you change the value of innerHTML, you must have valid HTML.

If
parentElem.innerHTML += "<td>Sample Text</td></tr></table>";
would have generated valid HTML, it must NOT have had valid HTML before  (it would have had to have an unmatched <table> tag!).

If you want to build HTML incrementally, do the incrementing in a string variable, and ONCE VALID HTML HAS BEEN ACHIEVED, copy it to innerHTML.
0
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 125 total points
ID: 16955179
Or simply said: after first assignment is your <table> closed internaly by a </table> tag automaticaly. Next html is then outside of that table.
Look for PageSpy to see the dynamic html automatic completition: http://www.experts-exchange.com/M_1344026.html
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Echo'd values in dropdowns 6 57
close pop up windows when logout or main window closes 17 86
Change background images after 5 seconds. 12 47
Drag & Drop Error 5 32
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 …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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