Fabri wrote:
I shoul want to know the content o a certain span:
************************************************** **********************
<html>
<head>
<title>Documento senza titolo</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1"> </head>
<script type="text/javascript">
function return_span_value(){
var s = document.getElementById("x");
alert(x.nodeName);
alert(x.nodeValue);
}
</script>
<body>
<span id="x">Hello world!</span>
<input type="button" value="Click to know span value"
onClick="return_span_value();">
</body>
</html>
************************************************** **********************
It should return "Hello world!" doesn't it?
Any help appreciated.
Regards.
--
Fabri
(Non è tuttu oru chiddu chi stralluci, né tuttu veru chiddu ca si
dici)
For starters - don't put a <script> between <head> & <body> tags.
It may not be a good idea to make any assumptions about what may be
appended to that span - could be nested elements, could be whitespace
nodes...innerHTML will return rich content, but is non-standard. Here's
another possibility.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Documento senza titolo</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript">
if (document.ELEMENT_NODE == null)
{
document.ELEMENT_NODE = 1;
document.TEXT_NODE = 3;
}
function normalizeString(s)
{
return s.replace(/\s\s+/g, " ").replace(/^\s*|\s*$/g, "");
}
function getTextValue(el)
{
var i;
var s = "";
for (i = 0; i < el.childNodes.length; i++)
if (el.childNodes[i].nodeType == document.TEXT_NODE)
s += el.childNodes[i].nodeValue;
else if (el.childNodes[i].nodeType == document.ELEMENT_NODE &&
el.childNodes[i].tagName == "BR")
s += " ";
else s += getTextValue(el.childNodes[i]);
return normalizeString(s);
}
function return_span_value(span_id)
{
var s = document.getElementById(span_id);
alert('span id: ' + s.id);
alert('span nodeName: ' + s.nodeName);
alert('s "text value": ' + getTextValue(s));
}
</script>
</head>
<body>
<span id="x1">Hello world!</span>
<input type="button" value="Click to know span value"
onClick="return_span_value('x1');">
<span id="x2"><a href="#null"><b>H</b>ello world!</a></span>
<input type="button" value="Click to know span value"
onClick="return_span_value('x2');">
</body>
</html>
//getTextValue courtesy Mike Hall
//http://www.brainjar.com/dhtml/tablesort/default4.asp
According to webreference:
The Document Object Model includes two relevant properties you can use:
data and nodeValue. You can use these properties to modify the content
of text nodes only. Both properties are set to the text node's value
and are fully interchangeable.