updating values with getelementsbyid

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DennyLoi
    New Member
    • Nov 2007
    • 10

    updating values 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:

    [CODE=javascript]
    function text(t)
    {
    return document.create TextNode(t);
    }
    function start(){

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

    function updateDetails()
    {
    document.getEle mentById('test' ) = text(getQuestio n());
    }
    [/CODE]
    //sometime later, call to updateDetails method.

    Unsurprisingly, this doesn’t work.

    Any advice?
    Last edited by gits; Nov 8 '07, 03:11 PM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    hi ...

    as far as i can see you create a textnode from a string. but with that you cannot do:

    [CODE=javascript]document.getEle mentById('test' ) = text(getQuestio n());[/CODE]
    you cannot work with dom-nodes that way i think. you simply could do:

    [CODE=javascript]document.getEle mentById('test' ).value = 'your_text';[/CODE]
    or you should have a look at the replaceChild() method ... the text of your element to replace is a textnode with a textvalue itself ... so you have to replace it accordingly ...

    kind regards

    Comment

    • DennyLoi
      New Member
      • Nov 2007
      • 10

      #3
      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).

      [CODE=javascript]function updateDetails()
      {
      var zzz = (document.getEl ementById('test '));
      zzz.removeChild (zzz.firstChild );
      zzz.appendChild (text(getQuesti on()));

      }[/CODE]

      This works, but doesn't seem very efficient. Is there a more suitable way?
      Last edited by gits; Nov 8 '07, 05:09 PM. Reason: added code tas

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        hi ...

        why do you use the complex textNode-way? you could set the value ... is there any reason that deserves the node-way for you?

        btw: please use code-tags when posting source code ...

        kind regards

        Comment

        Working...