Web Hosting Talk







View Full Version : Javascript


pztup
12-13-2004, 01:25 AM
I have this problem trying to get text with a span.

Lets say i have


<span id="testspan1">Hello World!</span>


How would I use Javascript to get text within the span (Hello World!)?

Akyriel
12-13-2004, 01:29 AM
One way would be to use the getElementById() method and innerHTML property.

myvar = document.getElementById('testspan1').innerHTML.value;

pztup
12-13-2004, 01:42 AM
I get undefined


<html>
<head>
</head>
<body>
<span id="testspan1">Hello World!</span>
<br>
<br>
Message inside:
<script>
myvar = document.getElementById('testspan1').innerHTML.value;
document.write(myvar);
</script>
</body>
</html>

Akyriel
12-13-2004, 01:58 AM
My head is foggy tonight, try this:

<html>
<head>
</head>
<body>
<span id="testspan1">Hello World!</span>
<br>
<br>
Message inside:
<script>
myvar = document.getElementById('testspan1').innerHTML;
document.write(myvar);
</script>
</body>
</html>


leave off the .value part

Akyriel
12-13-2004, 02:13 AM
Alternatively you can just print out the innerHTML with

<script>
document.write(document.getElementById('testspan1').innerHTML);
</script>

pztup
12-13-2004, 02:48 AM
Thanks that worked