getElementByID value

M

Michael Hill

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
 
K

kaeli

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";
 
G

Grant Wagner

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 said:
or it is:

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

--
| Grant Wagner <[email protected]>

* 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top