Connecting Tech Pros Worldwide Forums | Help | Site Map

getElementByID value

Michael Hill
Guest
 
Posts: n/a
#1: Jul 20 '05
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
Guest
 
Posts: n/a
#2: Jul 20 '05

re: getElementByID value


or it is:

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

Michael Hill wrote:
[color=blue]
> 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[/color]

Michael Hill
Guest
 
Posts: n/a
#3: Jul 20 '05

re: getElementByID value


or it is:

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

Michael Hill wrote:
[color=blue]
> 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[/color]

kaeli
Guest
 
Posts: n/a
#4: Jul 20 '05

re: getElementByID value


In article <3FBE3797.10F87FCC@ram.lmtas.lmco.com>,
hillmw@ram.lmtas.lmco.com enlightened us with...[color=blue]
> 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";
>[/color]

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

Grant Wagner
Guest
 
Posts: n/a
#5: Jul 20 '05

re: getElementByID value


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

document.getElementById("member_id").innerHTML

But:

document.getElementById("member_id").appendChild(d ocument.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(d ocument.createTextNode("Member"));

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

Michael Hill wrote:
[color=blue]
> or it is:
>
> document.getElementByID("member_id").innerHTML = "Member";
>
> Michael Hill wrote:
>[color=green]
> > 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[/color][/color]

--
| Grant Wagner <gwagner@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available
at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_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-deve...upgrade_2.html


Closed Thread