Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Writing HTML to a webpage using document.getElementById('results').childNodes[0].nodeValue

Reply
Thread Tools

Writing HTML to a webpage using document.getElementById('results').childNodes[0].nodeValue

 
 
Deryck
Guest
Posts: n/a
 
      09-18-2004
Hi,

I am trying to update part of a webpage using javascript. The bit that gets
updated will have a variable number of links written to it.

I have done this successfully by rewriting the whole page but this seems
like overkill (and a maintenance problem too potentially).

I thought about defining the area to be updated using a div. Something like
this:

<div id="results">Results will go here</div>

with the javascript doing something like:

var src = "blah blah blah";
document.getElementById('results').childNodes[0].nodeValue = src;

This works if src is just text. If it is HTML, say:

var src="<p><a href=\"some_file.html\">Some file</a></p>";

Then inside the div instead of getting a link to some_file.html I get the
raw HTML.

Is there either a way round this problem or a much better way of doing it in
the first place?

Thanks

Deryck


 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      09-18-2004


Deryck wrote:


> I thought about defining the area to be updated using a div. Something like
> this:
>
> <div id="results">Results will go here</div>
>
> with the javascript doing something like:
>
> var src = "blah blah blah";
> document.getElementById('results').childNodes[0].nodeValue = src;
>
> This works if src is just text. If it is HTML, say:
>
> var src="<p><a href=\"some_file.html\">Some file</a></p>";
>
> Then inside the div instead of getting a link to some_file.html I get the
> raw HTML.
>
> Is there either a way round this problem or a much better way of doing it in
> the first place?


Learn the DOM e.g.
var p = document.createElement('p');
var link = document.createElement('a');
a.href = 'some_file.html';
p.appendChild(a);

var resultsDiv = document.getElementById('results');
while (resultsDiv.hasChildNodes()) {
resultsDiv.removeChild(resultsDiv.lastChild);
}

resultsDiv.appendChild(p);

or help yourself with innerHTML

resultsDiv.innerHTML = '<p><a ...>...<\/a><\/p>';

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
Deryck
Guest
Posts: n/a
 
      09-18-2004

"Martin Honnen" <> wrote in message
news:414c162e$0$26103$...
>
>
> Learn the DOM e.g.


<Useful code snipped for brevity>

>


Thanks Martin...I appreciate your quick response.

Deryck


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cause webpage one to reload when webpage two is closed. Paul ASP .Net 14 06-19-2008 03:02 PM
html - webpage using frames, a small problem Rolf Computer Support 4 04-20-2007 08:53 AM
Clipping a remote webpage with Javascript/XPath and including in a "local" webpage soren625 Javascript 2 12-12-2006 02:09 PM
check if a webpage is forwarding to a other webpage martijn@gamecreators.nl Python 1 09-06-2005 02:27 PM
Email contents of webpage or Form on webpage w/o using Server scripting sifar Javascript 5 08-24-2005 05:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57