getElementByID value

Discussion in 'Javascript' started by Michael Hill, Nov 21, 2003.

  1. Michael Hill

    Michael Hill Guest

    I have this tag:

    <span id="member_id"></span>

    and I want to change the value to "Member"

    why can't I do:

    document.getElementByID("member_id").value = "Member";

    Any help is appreciated.

    Mike
     
    Michael Hill, Nov 21, 2003
    #1
    1. Advertising

  2. Michael Hill

    Michael Hill Guest

    or it is:

    document.getElementByID("member_id").innerHTML = "Member";

    Michael Hill wrote:

    > I have this tag:
    >
    > <span id="member_id"></span>
    >
    > and I want to change the value to "Member"
    >
    > why can't I do:
    >
    > document.getElementByID("member_id").value = "Member";
    >
    > Any help is appreciated.
    >
    > Mike
     
    Michael Hill, Nov 21, 2003
    #2
    1. Advertising

  3. Michael Hill

    Michael Hill Guest

    or it is:

    document.getElementByID("member_id").innerHTML = "Member";

    Michael Hill wrote:

    > I have this tag:
    >
    > <span id="member_id"></span>
    >
    > and I want to change the value to "Member"
    >
    > why can't I do:
    >
    > document.getElementByID("member_id").value = "Member";
    >
    > Any help is appreciated.
    >
    > Mike
     
    Michael Hill, Nov 21, 2003
    #3
  4. Michael Hill

    kaeli Guest

    In article <>,
    enlightened us with...
    > I have this tag:
    >
    > <span id="member_id"></span>
    >
    > and I want to change the value to "Member"
    >
    > why can't I do:
    >
    > document.getElementByID("member_id").value = "Member";
    >


    There is no value property to a span element.
    I assume you want the text "Member" displayed in that span.

    var e = document.getElementById("member_id");
    var oTextNode = document.createTextNode("Member");
    var oReplaceNode = e.childNodes(0);
    oReplaceNode.replaceNode(oTextNode);

    You could also use innerHTML, but I have heard it isn't cross-browser
    enough. It would be

    document.getElementById("member_id").innerHTML = "Member";

    --
    ~kaeli~
    Why do they sterilize the needles for lethal injections?
    http://www.ipwebdesign.net/wildAtHeart
    http://www.ipwebdesign.net/kaelisSpace
     
    kaeli, Nov 21, 2003
    #4
  5. Michael Hill

    Grant Wagner Guest

    It's getElementById(), not getElementByID(), so it would be:

    document.getElementById("member_id").innerHTML

    But:

    document.getElementById("member_id").appendChild(document.createTextNode("Member"));

    is more standards compliant. Please note that the line above is
    only required when initially appending text to the empty span.
    Once the text node exists, you only need to change it's value. If
    you're certain that the span will always only contain a text
    node, you could use something like:

    if (document.getElementById("member_id").firstChild == null) {

    document.getElementById("member_id").appendChild(document.createTextNode("Member"));

    } else {
    document.getElementById("member_id").firstChild.nodeValue =
    "Something Else";
    }

    Michael Hill wrote:

    > or it is:
    >
    > document.getElementByID("member_id").innerHTML = "Member";
    >
    > Michael Hill wrote:
    >
    > > I have this tag:
    > >
    > > <span id="member_id"></span>
    > >
    > > and I want to change the value to "Member"
    > >
    > > why can't I do:
    > >
    > > document.getElementByID("member_id").value = "Member";
    > >
    > > Any help is appreciated.
    > >
    > > Mike


    --
    | Grant Wagner <>

    * Client-side Javascript and Netscape 4 DOM Reference available
    at:
    *
    http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

    * Internet Explorer DOM Reference available at:
    *
    http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

    * Netscape 6/7 DOM Reference available at:
    * http://www.mozilla.org/docs/dom/domref/
    * Tips for upgrading JavaScript for Netscape 7 / Mozilla
    * http://www.mozilla.org/docs/web-developer/upgrade_2.html
     
    Grant Wagner, Nov 21, 2003
    #5
    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. Rob
    Replies:
    4
    Views:
    632
    Patrice
    Jan 23, 2008
  2. lawrence
    Replies:
    13
    Views:
    307
    Thomas 'PointedEars' Lahn
    Sep 4, 2004
  3. weston
    Replies:
    19
    Views:
    225
  4. edai
    Replies:
    3
    Views:
    128
  5. ll
    Replies:
    2
    Views:
    340
    GArlington
    Aug 22, 2008
Loading...

Share This Page