473,789 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>so me <strong>importa nt</strong> stuff</p></div>

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

the expect rendering would be:
<div id='Div1'><h1>Y ay!</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 1899
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 createContextua lFragment:

<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>so me <strong>importa nt</strong> stuff</p></div>

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

the expect rendering would be:
<div id='Div1'><h1>Y ay!</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
1732
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
2186
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 contains this HTML: <div id=”oDiv” style="margin-bottom:10px;">{when}</div> <div> <table style="border:1px solid gray"> <tr> <td>blah</td>
7
4928
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 valign="top">my text </td> </tr> <tr> <td><img src="spacer.gif" width="140" height="1" /></td>
12
5930
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: <gibberish>file//g:\pathtofile1<gibberish>file//g:\pathtofile2<gibberish> etc. I want to remove the "g:\" from the file paths. I wrote a console app that successfully reads the file and writes a duplicate of it, but fails for some reason to do the "replacing" of the "g:\". The code...
6
24375
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 XSLT and replace all occurances of &lt; with < and &gt; with >.
6
16383
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 align="center" valign="middle" width="173" height="100" bgcolor="#ffffff" class="bdDkBluBld" colspan="3"> I am told that the following are "outdated and newer constructs are available": width
5
2405
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 three bolded periods '...' in search-result listings. Additionally, most content is outdated; as many users need up-to-date content. Hence, filtering-through search-results becomes quite cumbersome. The newsgroup listings allow detailed...
9
2986
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 got the core of the javascript from here: http://www.dynamicdrive.com/dynamicindex17/ajaxcontent.htm I noticed in the demo that sometimes the content takes a long
1
2561
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 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt; <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>1920s to 1930s high-style Vintage Clothing at Vintage Textile</title>
0
9663
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9506
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10404
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10193
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6761
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.