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

Discussion in 'Javascript' started by Deryck, Sep 18, 2004.

  1. Deryck

    Deryck Guest

    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
     
    Deryck, Sep 18, 2004
    #1
    1. Advertising

  2. 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/
     
    Martin Honnen, Sep 18, 2004
    #2
    1. Advertising

  3. Deryck

    Deryck Guest

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


    <Useful code snipped for brevity>

    >


    Thanks Martin...I appreciate your quick response.

    Deryck
     
    Deryck, Sep 18, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lb
    Replies:
    7
    Views:
    10,629
    TechBookReport
    Apr 28, 2004
  2. yawnmoth

    nodeValue vs. textContent

    yawnmoth, Dec 7, 2007, in forum: XML
    Replies:
    2
    Views:
    4,527
    Martin Honnen
    Dec 8, 2007
  3. wk
    Replies:
    5
    Views:
    192
    Dietmar Meier
    Apr 22, 2005
  4. Martin
    Replies:
    3
    Views:
    165
  5. Replies:
    4
    Views:
    178
    Jonas Raoni
    Nov 21, 2006
Loading...

Share This Page