473,651 Members | 3,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to know span content???

I shoul want to know the content o a certain span:
*************** *************** *************** *************** ************

<html>
<head>
<title>Document o senza titolo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<script type="text/javascript">
function return_span_val ue(){
var s = document.getEle mentById("x");
alert(x.nodeNam e);
alert(x.nodeVal ue);
}
</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)
Jul 23 '05 #1
6 14096
*** Fabri wrote/escribió (Thu, 17 Mar 2005 10:03:56 +0100):
function return_span_val ue(){
var s = document.getEle mentById("x");
alert(x.nodeNam e);
alert(x.nodeVal ue);
}


Try:

alert(x.innerHT ML);
--
-- Álvaro G. Vicario - Burgos, Spain
-- Don't e-mail me your questions, post them to the group
--
Jul 23 '05 #2
Fabri wrote:

[snip]
var s = document.getEle mentById("x");
alert(x.nodeNam e);
alert(x.nodeVal ue);
I assume you meant s.node<...>

[snip]
It should return "Hello world!" doesn't it?


No. The SPAN element itself doesn't contain the text; it has a child
text node and /that/ contains the text:

/* Check for getElementById support before using the method. */
if(document.get ElementById) {
var s = document.getEle mentById('x'), tN;

/* Check that s is a valid reference, and that it
* has non-null object reference in firstChild.
*/
if(s && (tN = s.firstChild)) {
/* tN now references the text node within the SPAN.
* Displaying tN.nodeName should reveal '#text'.
*
* Both the data and nodeValue properties
* of a text node return the text within.
*/
alert(tN.data); // 'Hello world!'
}
}

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
Michael Winter wrote:
alert(tN.data);


Also

alert(tN.nodeVa lue);

works.

Regards.

--
Fabri
(Non è tuttu oru chiddu chi stralluci, né tuttu veru chiddu ca si dici)
Jul 23 '05 #4
<snip>
both the data and nodeValue properties
* of a text node return the text within.
*/
and with that Michael would you be kind enough to explain the difference and
when to use one or the other? I find nothing definitive. TIA
jimbo


ael Winter Replace ".invalid" with ".uk" to reply by e-mail.

Jul 23 '05 #5
Fabri wrote:
I shoul want to know the content o a certain span:
*************** *************** *************** *************** ************
<html>
<head>
<title>Document o senza titolo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head>
<script type="text/javascript">
function return_span_val ue(){
var s = document.getEle mentById("x");
alert(x.nodeNam e);
alert(x.nodeVal ue);
}
</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...innerHT ML 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>Document o senza titolo</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript">

if (document.ELEME NT_NODE == null)
{
document.ELEMEN T_NODE = 1;
document.TEXT_N ODE = 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.l ength; i++)
if (el.childNodes[i].nodeType == document.TEXT_N ODE)
s += el.childNodes[i].nodeValue;
else if (el.childNodes[i].nodeType == document.ELEMEN T_NODE &&
el.childNodes[i].tagName == "BR")
s += " ";
else s += getTextValue(el .childNodes[i]);
return normalizeString (s);
}

function return_span_val ue(span_id)
{
var s = document.getEle mentById(span_i d);
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.co m/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 .

Jul 23 '05 #6
J. J. Cale wrote:
<snip>
* both the data and nodeValue properties
* of a text node return the text within.
*/


and with that Michael would you be kind enough to explain the difference and
when to use one or the other?


As far as I know, there isn't one. Both Node and CharacterData/Text
are fundamental interfaces (so they should be implemented by all
conforming user agents) and both data and nodeValue share the same
exception behaviour. Just pick which one you prefer. I use data
because it's unique and so it should be obvious (if not already) that
code is dealing with a text node.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
6004
by: deko | last post by:
How to change a link's color based on a php variable? I have a number of links on a page: <p><a href="page1.php">Page 1</a></p> <p><a href="page2.php">Page 2</a></p> <p><a href="page3.php">Page 3</a></p> <p><a href="page4.php">Page 4</a></p> I want the link to be red if $nav has a value that corresponds to the page
9
2601
by: Tim Lister | last post by:
I'm used to seeing crappy pages with FONT tags littered everywhere, even for some bizarre reason when CSS is in use also, so i was less than ecstatic when i saw it happening in yet another page. however, on closer examination i saw the construct: <font class="some_damn_thing"> ... </font> and THIS was what was stuck in all over the place.
23
4072
by: Mikko Ohtamaa | last post by:
From XML specification: The representation of an empty element is either a start-tag immediately followed by an end-tag, or an empty-element tag. (This means that <foo></foo> is equal to <foo/>) From XHTML specification:
3
39716
by: ShadowMan | last post by:
Hi all I have something like <DIV style="border: 1px solid black"> <SPAN>left side text</span> <span>right side text</span> </div> Using CSS I would like to aling first SPAN on left and second SPAN (on the same line) on right side. Using float property I can do it but, text appears outside DIV border.
6
2827
by: hsomob1999 | last post by:
so i have a <ul> and I allow the user to append items to it. The problem is that on mozilla the <span class="line"> which is just a line to divide the sections gets overlaped and doesnt move down and adjust to the newly added items like it does in iE. It just occured to me that i dont really have to use a span, and a html <hr> tag could do the trick -I will go try. But aside from that could some one explain why this occurs? And will I get...
5
3246
by: ste.paoletti | last post by:
I have a problem with css I have a this xhtml code: <span> <span> <span/> <input type="radio"/> .... <span/> <input type="button" onclick ="var s=document.createElement('span'); this.previousSibling.firstChild.appendChild(s); "/>
2
3940
by: ricky | last post by:
Hello, If anyone could help me with this I would highly appreciate it. I've tried everything and nothing works. What I am trying to do is so damn basic and it's just frustrating that it seems there's no support for this. Either that or I'm doing something wrong. Well, enough venting, here's what I need. Using this sample XML file (test.xml):
3
1697
by: Steve | last post by:
Hi; I'm working on a demo of using a timer on a web site that is made visible by making a div visible. My "PopIn Box" div is empty on the page. Before making it visible I used javascript to get the content from another hidden div. I'm doing it this way to make it easier to add in more messages by simply putting more hidden divs on the page.
0
8277
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8803
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8700
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8581
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4285
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1910
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.