473,386 Members | 1,873 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,386 software developers and data experts.

Replacing content of DIV with arbitrary HTML markup

Hi,
Using javascript, I would like to replace the contents of a DIV with
new, arbitrary HTML markup. I want to write a W3C-compliant function
for doing so; it would look something like this:

function changeContents(divElement, htmlMarkup)
{
// divElement: an existing DIV object reference
// htmlMarkup: new HTML markup to replace whatever is there
}

Consider the following code:
<div id='Div1'><p>some <strong>important</strong> stuff</p></div>

If I called my function like so:
changeContents(document.getElementById('Div1'),'<h 1>Yay!</h1>');

the expect rendering would be:
<div id='Div1'><h1>Yay!</h1></div>

I've read many suggestions using nodes manipulation, but all of them
seemed to work only if you're just adding plain text -- not HTML tags.
In the latter case, I've seen suggestions to use createElement() which
is fine when you *know* exactly what you're adding... but what about a
function?

I hope I'm making sense here... Any ideas will be greatly appreciated,
Thanks!

Aug 29 '05 #1
2 1872
Terry wrote:
Hi,
Using javascript, I would like to replace the contents of a DIV with
new, arbitrary HTML markup. I want to write a W3C-compliant function
for doing so; it would look something like this:

function changeContents(divElement, htmlMarkup)
{
// divElement: an existing DIV object reference
// htmlMarkup: new HTML markup to replace whatever is there
}
Sorry, W3C compliance is not possible.

The best solution is probably to use innerHTML. It has been widely if
inconsistently implemented and therefore has some foibles: you need to
be careful what you do.

One limitation is that you must not attempt to modify tables, though you
can modify cell content or create entire tables.

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

The Mozilla equivalent is createContextualFragment:

<URL:http://www.mozilla.org/docs/dom/domref/dom_range_ref26.html#1005287>

But it is not well supported outside Mozilla/Firefox/Netscape browsers
(if at all).

Consider the following code:
<div id='Div1'><p>some <strong>important</strong> stuff</p></div>

If I called my function like so:
changeContents(document.getElementById('Div1'),'<h 1>Yay!</h1>');

the expect rendering would be:
<div id='Div1'><h1>Yay!</h1></div>


Then changeContents might be:

function changeContents( el, markUp ) {
el.innerHTML = markUp;
}

But there is no error checking or recovery for bad markup.

[...]

--
Rob
Aug 29 '05 #2

Original :: RobG :: 2005-08-29 01:06

Terry wrote:
Hi,
Using javascript, I would like to replace the contents of a DIV with
new, arbitrary HTML markup. I want to write a W3C-compliant function
for doing so; it would look something like this:

function changeContents(divElement, htmlMarkup)
{
// divElement: an existing DIV object reference
// htmlMarkup: new HTML markup to replace whatever is there
}


Sorry, W3C compliance is not possible.

The best solution is probably to use innerHTML. It has been widely if
inconsistently implemented and therefore has some foibles: you need to
be careful what you do.


Thanks RobG.

The goal of the function was to implement HTML tooltips in a
single page. I ended up writing each tooltip as an
individual DIV containing all desired markup. Initially
invisible, the appropriate tooltip was repositioned and made
visible/invisible using the onmouseover/onmouseout events of
a span. Not exactly revolutionary but it gets the job done
-- and this can be further automated using server-side or
client-side embellishments, of course.
Aug 30 '05 #3

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

Similar topics

7
by: Rob Meade | last post by:
Hi all, Been a long time since I've been here... /me waves to all.. Ok - my conundrum.. I have a form where a user can enter text and BB codes...for example:
1
by: Andrew Poulos | last post by:
Say I have a page, which has been created by a third party, and the page may contain some pre-specified text. How can I find and replace that text dynamically? For example, if the page I have...
7
by: Wilhelm Kutting | last post by:
Hi again i want to replace the following layouttabel with css/xhtml: ---------------------------------- <table width="640" cellspacing="0"> <tr> <td valign="top">My Title</td> <td...
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
6
by: tentstitcher | last post by:
Hi all: I have a source xml document with an element of type string. This element contains something like the following: <stringData> &lt;Header&gt; &lt;Body&gt; </stringData> I would like to apply an...
6
by: silverbob | last post by:
I am evaluating EWD, which seems to be an improvement over FrontPage. In code view, the program alerts you to items that are not HTML 4.0 compliant. For example, in this line... <td...
5
by: justobservant | last post by:
When more than one keyword is typed into a search-query, most of the search-results displayed indicate specified keywords scattered throughout an entire website of content i.e., this is shown as...
9
by: pbd22 | last post by:
Hi. This is just a disaster management question. I am using XMLHTTP for the dynamic loading of content in a very crucial area of my web site. Same as an IFrame, but using XMLHTTP and a DIV. I...
1
by: cma6 | last post by:
I have a page which uses tables for layout http://www.vintagetextile.com/1920s_to_1930s.htm The checkerboard pattern is achieved with this markup: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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,...

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.