updating values with getelementsbyid

Discussion in 'JavaScript' started by DennyLoi, Nov 8, 2007.

  1. #1
    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?
     
    DennyLoi, Nov 8, 2007 IP
  2. KatieK

    KatieK Active Member

    Messages:
    116
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    58
    #2
    I think the part of the problem is this:

    document.getElementById('test') = text(getQuestion());
    Code (markup):
    document.getElementById('test') is only the reference to an element. It sounds like you want to change the value of that element. So if 'test' is an input field, you would use this to change it:

    document.getElementById('test').value = "foobar";
    Code (markup):
    It would change the contents of the input field to "foobar" for instance.
     
    KatieK, Nov 8, 2007 IP
  3. sin0cide

    sin0cide Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think what you looking for is a

    document.getElementById('test').innerHTML  = "Code to be input"
    Code (markup):
     
    sin0cide, Nov 9, 2007 IP
  4. rena

    rena Peon

    Messages:
    1,987
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ceck this part ,, here si problem
    var oHelloText = text(getQuestion()); //the getQuestion method returns a string from an array
    oHelloText.id = "test";
     
    rena, Nov 9, 2007 IP