473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Best method to add HTML dynamically

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

Oct 22 '06 #1
16 3029
li************@hotmail.com said the following on 10/22/2006 5:49 PM:
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?
Use DynWrite as in the group FAQ.
<URL: http://jibbering.com/faq/#FAQ4_15>

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Oct 22 '06 #2
Randy Webb wrote:
li************@hotmail.com said the following on 10/22/2006 5:49 PM:
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?

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-HTM...ml#ID-39872903 >
<URL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-68927016 >

--
Rob

Oct 22 '06 #3
ASM
RobG a écrit :
>
<URL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-39872903 >
<URL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-68927016 >
what does mean 'IDL' in 'IDL definition' ?
Oct 23 '06 #4
ASM wrote:
RobG a écrit :

<URL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-39872903 >
<URL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-68927016 >

what does mean 'IDL' in 'IDL definition' ?
Interface Definition Langauge:

"a generic term for a language that lets a program or object
written in one language communicate with another program
written in an unknown language."

<URL:
http://whatis.techtarget.com/definit...212314,00.html >
--
Rob

Oct 23 '06 #5
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

Oct 23 '06 #6
ASM
li************@hotmail.com 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.mori...nerHTML_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>

--
ASM
Oct 23 '06 #7

Randy Webb wrote:
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.

Oct 23 '06 #8
ASM
Andy Dingley <di*****@codesmiths.coma écrit :
>
Then the FAQ needs an update. DynWrite uses .innerHTML, which is surely
not a good thing to be encouraging in new code.
simple example to add an element with DOM :
http://perso.orange.fr/stephane.moriaux/truc/add_input

--
ASM
Oct 23 '06 #9
li************@hotmail.com wrote:
>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?
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/a.../innerhtml.asp
>
>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?
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). :-)
--
Rob
Oct 23 '06 #10
In message <HZ********************@telcove.net>, Sun, 22 Oct 2006
19:01:08, Randy Webb <Hi************@aol.comwrites
>li************@hotmail.com said the following on 10/22/2006 5:49 PM:
> I have a dynamic table which is populated using Javascript.
Currently I
am looking at adding nodes using the W3C DOM,
>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
??????????????????

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 23 '06 #11
J R Stockton said the following on 10/23/2006 7:27 AM:
In message <HZ********************@telcove.net>, Sun, 22 Oct 2006
19:01:08, Randy Webb <Hi************@aol.comwrites
>comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
??????????????????
Yikes! That is the result of me not paying attention to my signature :\

Guess my mind wasn't prepared enough....
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Oct 24 '06 #12
Andy Dingley <di*****@codesmiths.comsaid the following on 10/23/2006
7:40 AM:
Randy Webb wrote:
>Use DynWrite as in the group FAQ.
<URL: http://jibbering.com/faq/#FAQ4_15>

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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Oct 24 '06 #13
li************@hotmail.com said the following on 10/23/2006 4:23 AM:
>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?
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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Oct 24 '06 #14
ASM
RobG a écrit :
li************@hotmail.com wrote:
>I see that innerHTML is an MS extension. Is there sufficient cross
browser support to warrent using it?
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/a.../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
Oct 24 '06 #15

ASM wrote:
RobG a écrit :
li************@hotmail.com wrote:
I see that innerHTML is an MS extension. Is there sufficient cross
browser support to warrent using it?

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/a.../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
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-HTM...ml#ID-64060425 >

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

--
Rob

Oct 24 '06 #16
ASM
RobG a écrit :
ASM wrote:
<http://msdn.microsoft.com/workshop/author/tables/buildtables.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

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-HTM...ml#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.
Oct 24 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: James | last post by:
What is the best method for creating a Web Page that uses both PHP and HTML ? <HTML> BLA BLA BLA BLA BLA
2
by: Jeff Uchtman | last post by:
What's the best method to hide, or not show a certain record when doing a pull from a database, as in I am pulling a list with a dozen category's and I don't what to show one of them. Thanks...
2
by: John Smith | last post by:
Hey folks, I'm writing a Windows application which has many forms which have Datagrids on them to display data. These datagrids will not be editable in anyway. They are there to just view the...
1
by: msnews.microsoft.com | last post by:
I'd like to hear your thoughts on best methods for populating drop down list controls. I have states and countries drop down lists that don't change often, so naturally I "hard code" them in the...
18
by: blueapricot416 | last post by:
I am on a team designing the front end of an Intranet application. The backend guys will be using JSP and AJAX-like functionality to make server requests that must dynamically change elements on a...
2
by: Fred Flintstone | last post by:
What's the difference between these two methods? 1 - Parameterrized SQL queries: Dim CommandObject As New Data.SqlClient.SqlCommand With CommandObject .Connection = myConnection...
12
by: howachen | last post by:
I have many text file with 1 to 2MB in size currently, i use the method: echo file_get_contents( $file_path ); are there any better method? thanks.
3
by: R.A.F. | last post by:
Hi, From C++ world, i'm used to have a cpp file, where i placed all common functions that i could need within different projects. something like my own "API". i would like to do something...
2
by: Steven Cheng [MSFT] | last post by:
Hi Cj, I also noticed that the "Act" variable here seems hasn't been declared(or declared anywhere else?). I've try building the same code and got the same result and the "Operator '+' cannot be...
5
by: chromis | last post by:
Hi, I'm trying to figure out the best method for uploading a number of files along with entering some data into a database using a component containing CRUD methods. I have the following file...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.