Best method to add HTML dynamically

L

listerofsmeg01

Hi all,

New to Javascript, and want to learn to do things the RIGHT way.

I have a dynamic table which is populated using Javascript. Currently I
am looking at adding nodes using the W3C DOM, but these seems pretty
long winded, especially adding all the attributes etc.

Is there a way to just blat in a lump of HTML text, or is this frowned
upon now?

Cheers,
Lister
 
R

RobG

Randy said:
(e-mail address removed) said the following on 10/22/2006 5:49 PM:

Use DynWrite as in the group FAQ.
<URL: http://jibbering.com/faq/#FAQ4_15>

DynWrite is based on innerHTML, which is not the best way to modify
tables. It is OK when used to write an entire table, or the contents of
a cell, but it should never be used for the components of a table
(tableSection, tableRow, etc.).


For the OP:

insertRow and insertCell are quite efficient ways to add elements to a
table. Using CSS to style them can save on table-building code. You
might also find it suitable to clone entire rows, then modify the
content of cells.

<URL: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-39872903 >
<URL: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-68927016 >
 
L

listerofsmeg01

DynWrite is based on innerHTML, which is not the best way to modify
tables. It is OK when used to write an entire table, or the contents of
a cell, but it should never be used for the components of a table
(tableSection, tableRow, etc.).

I see that innerHTML is an MS extension. Is there sufficient cross
browser support to warrent using it?

Out of interest, why shouldn't it be used for tables?
insertRow and insertCell are quite efficient ways to add elements to a
table. Using CSS to style them can save on table-building code.

Well I'm afraid I got so hung up on "not using tables for layout", that
I created this particular table using CSS, even though it is a
perfectly valid table - d'oh!
Is InnerHTML OK in this instance?

Many thanks,
Lister
 
J

J R Stockton

Sun said:
(e-mail address removed) said the following on 10/22/2006 5:49 PM:
Use DynWrite as in the group FAQ.
<URL: http://jibbering.com/faq/#FAQ4_15>

ISTM that being able to use the W3C DOM may imply, in practice, being
able to use getElementById and not needing document.all.

If so, IMHO, there should also be in 4.15 a shorter DynWrite using only
gEBI. It's still worth having the function (in an include file), for
compactness and legibility of code in Web pages. Only rarely will it be
necessary to cache the gEBI result in a variable.

comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
??????????????????
 
A

ASM

(e-mail address removed) a écrit :
Well I'm afraid I got so hung up on "not using tables for layout", that
I created this particular table using CSS, even though it is a
perfectly valid table - d'oh!
Is InnerHTML OK in this instance?

lost url to your page

innerHTML would have to be use only with what is between tags (between
onening and closind)

Example of malfunction :
http://perso.orange.fr/stephane.moriaux/truc/innerHTML_danger

A work about adding row(s) to a table,
I don't know if it is correct with IE :
<http://perso.orange.fr/stephane.moriaux/internet/web_dom/clones/test_clones_css_dom_min>
 
A

Andy Dingley

Randy said:
Use DynWrite as in the group FAQ.
<URL: http://jibbering.com/faq/#FAQ4_15>

Then the FAQ needs an update. DynWrite uses .innerHTML, which is surely
not a good thing to be encouraging in new code.

The "right" way is to use the DOM methods, even though they're
long-winded. Wrap them up as needed to keep the coding effort down.
 
R

RobG

I see that innerHTML is an MS extension. Is there sufficient cross
browser support to warrent using it?

Out of interest, why shouldn't it be used for tables?

Using innerHTML on table row or section elements will cause errors in IE
at least. Microsoft's documentation says:

"To change the contents of the table, tFoot, tHead, and tr
elements, use the table object model described in How to
Build Tables Dynamically."

<URL:
http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/innerhtml.asp
Well I'm afraid I got so hung up on "not using tables for layout", that
I created this particular table using CSS, even though it is a
perfectly valid table - d'oh!
Is InnerHTML OK in this instance?

It has nothing to do with what you use tables for, only how you modify
them (innerHTML is just a property, it can't differentiate whether the
table is being used for tabular data or just layout). :)
 
R

Randy Webb

J R Stockton said the following on 10/23/2006 7:27 AM:
??????????????????

Yikes! That is the result of me not paying attention to my signature :\

Guess my mind wasn't prepared enough....
 
R

Randy Webb

Andy Dingley said:
Then the FAQ needs an update.

With regards to Tables, yes. DynWrite is a poor way to try to modify
tables. That was an error on my part of referring to it without
realizing the OP was modifying Tables.
DynWrite uses .innerHTML, which is surely not a good thing to
be encouraging in new code.

Sure it is. It is very widely supported. I don't know of a "modern"
browser (read - under 5 years old) that doesn't support it.
The "right" way is to use the DOM methods, even though they're
long-winded. Wrap them up as needed to keep the coding effort down.

If it were that simple, it would already be in the FAQ. The problem is
the long-winded solution it would take to emulate innerHTML using DOM
methods. You would basically have to write an HTML parser in JS. Not
pretty and not worthwhile for the FAQ.
 
R

Randy Webb

(e-mail address removed) said the following on 10/23/2006 4:23 AM:
I see that innerHTML is an MS extension. Is there sufficient cross
browser support to warrent using it?

Yes. It is very widely supported in modern browsers.
Out of interest, why shouldn't it be used for tables?

It whacks them out and throws errors. It was a mistake on my part of
referring to it for use with tables.
 
A

ASM

RobG a écrit :
Only to feed/delete/change content of a cell (a TD) (if this content is
not a form element), or caption.
"To change the contents of the table, tFoot, tHead, and tr
elements, use the table object model described in How to
Build Tables Dynamically."

<URL:
http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/innerhtml.asp

( createTHead/deleteTHead createTFoot/deleteTFoot
createCaption/deleteCaption insertRow/deleteRow insertCell/deleteCell)

Do all these instructions (smelling specific IE's) are supported by
other browsers ? and if yes witch ones
 
R

RobG

ASM said:
RobG a écrit :

Only to feed/delete/change content of a cell (a TD) (if this content is
not a form element), or caption.


( createTHead/deleteTHead createTFoot/deleteTFoot
createCaption/deleteCaption insertRow/deleteRow insertCell/deleteCell)

Do all these instructions (smelling specific IE's) are supported by
other browsers ? and if yes witch ones

They are all W3C specified methods of the HTML table, table section and
table row element interfaces.

<URL: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-64060425 >

The MSDN stuff also tells you which ones are W3C and those that aren't.
 
A

ASM

RobG a écrit :
ASM wrote:

They are all W3C specified methods of the HTML table, table section and
table row element interfaces.

<URL: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-64060425 >

Super !
The MSDN stuff also tells you which ones are W3C and those that aren't.

I missed that :-(
Saw : list of create/delete
then : long parts of code using createElement("TR"); and so on
as they would be 2 worlds (independent) IE <-> W3C.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top