View Full Version : displaying text in span

sir pannels
12-26-2006, 05:54 PM
hi all,

Im trying to set the text inside a span when i click a button ..

the span is

<spand id=sText></span>

and the button just triggers onclick call to 'building' as below...

function building() {
sText.value="test info";

but nothing happens when I click...

have I missed something? ny pointers be very much apperciated.

cheers very much
Sir P

Mr J
12-26-2006, 06:43 PM
You have a typo in your opening span tag

<spand id=sText></span>

and you should be referencing as

document.getElementById("sText").innerHTML="test info";

12-26-2006, 07:34 PM
If its just text you are trying to set then I would do something like this instead

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
function setText(where,what)
document.getElementById(where).firstChild.nodeValue = what;
<input type="button" onclick="setText('sText','test info 1');" value="Set Text 1"><br>
<input type="button" onclick="setText('sText2','test info 2');" value="Set Text 2"><br>
<input type="button" onclick="setText('sText3','test info 3');" value="Set Text 3"><br>
<!--something needs to be in the span for the function to work-->
<span id="sText">&nbsp;</span><br>
<span id="sText2">&nbsp;</span><br>
<span id="sText3">&nbsp;</span></div>