Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Incrementing the innerHTML property like a string

Posted on 2006-06-21
2
Medium Priority
?
369 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 375 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 375 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

670 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