SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    updating elements with getelementsbyid

    Hi everyone,

    I am trying to update the value which is stored in a particular part of my page by using the getElementsById method, followed by calling a function to update the field. However, I donít know how to do this. The relevant parts of my code are as follows:


    function text(t)
    {
    return document.createTextNode(t);
    }
    function start(){

    var oHelloText = text(getQuestion()); //the getQuestion method returns a string from an array
    oHelloText.id = "test";
    }

    function updateDetails()
    {
    document.getElementById('test') = text(getQuestion());
    }

    //sometime later, call to updateDetails method.

    Unsurprisingly, this doesnít work.

    Any advice?

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You can't give Text Nodes IDs. You have to give an element the ID and the element then contains the text node. You can append the text node to the element using appendChild.

  3. #3
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see.

    I went a layer up and gave the id to my P, which contains the text node.

    I then wrote this (where 'test' now refers to the holder of the text node).

    function updateDetails()
    {
    var zzz = (document.getElementById('test'));
    zzz.removeChild(zzz.firstChild);
    zzz.appendChild(text(getQuestion()));

    }

    This works, but doesn't seem very efficient. Is there a more suitable way?

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function updateDetails()
    {
    var zzz = (document.getElementById('test'));
    zzz.firstChild.nodeValue = text(getQuestion);
    }
    or even:
    Code:
    function updateDetails()
    {
    document.getElementById('test').firstChild.nodeValue = text(getQuestion);
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •