Solved

Incrementing the innerHTML property like a string

Posted on 2006-06-21
2
363 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
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

760 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

24 Experts available now in Live!

Get 1:1 Help Now