Reading value from label (DIV) with JS

M

Mateo

Hi!

I have labels on my page (some are web forms controls, and some are plain
HTML labels), and I need
Mozilla compatibile way for reading label value.

For example, in IE I can use this:
value = document.form1.lblMyLabelControl.innerText;
but, innerText property is not compatibile with W3 DOM, so mozilla doesn't
support this.

There is also innerHTML property, whichis not compatibile with W3 DOM, but
mozilla
supports this property (?!!?!). But this is not my favorite becouse it
returns complete HTML
between start and end tag, and I need only string text.

It looks like .value or .text properties doesn't exist for label control, so
I cannot use them...
There is "nodeValue" property which doesn't return what I need (and I need
only text contained
in label control)....

So, it looks like it's pretty hard to find method for getting text from
label which works in Mozilla and IE?!
Or I'm pretty stupid or tired to find solution for this SIMPLE problem.


(It would be also nice to find universal way for setting text for label or
values in INPUT fields,
but I would be pretty satisfied only with reading text from label...)

Thank you guys...
 
I

Ivo

For example, in IE I can use this:
value = document.form1.lblMyLabelControl.innerText;
but, innerText property is not compatibile with W3 DOM, so mozilla doesn't
support this.

Try this:
value = document.form1.lblMyLabelControl.firstChild.nodeValue;
or:
value = document.form1.lblMyLabelControl.firstChild.data;

The first child of the element is the textnode (if it isn't, the element
contains other stuff too, and you need to navigate the DOM some more),
which has a property "nodeValue" containing the text you are looking for.
All W3 DOM-compatible, all Mozilla-fähig.
It would be also nice to find universal way for setting text for label or
values in INPUT fields, but I would be pretty satisfied only with reading
text from label...

This nodeValue thing is even read/write, so a statement like:

document.form1.lblMyLabelControl.firstChild.nodeValue = 'Some new text';

would set the displayed text to, well, some new text.

There are other ways.
hth
ivo
http://4umi.com/
 
M

Mateo

Thx man!

Still there is one problem with this approach... And the problem is like you
said, when
element contains other stuff... then I'm not quite sure that first child
node is textnode.

I have huge code, and I prefer not to change it, which goes through elements
on page,
and reading/changing values according to some rules. Somethimes there is
posibility
that current control is not empty (label is actually DIV, and DIV is
container) becouse
those are dinamically generated controls in very complex asp.net
application.

Someone wrote this code last year and left the company, and I'm trying to
make it Mozilla
compatibile without making changes in concepts and algorithms.

So if u have any other idea besides firstChild.nodeValue I would be very
grateful.....
If not, I can try to use this with some aditional handling....
Thx anywway...
 
R

RobG

Mateo said:
Thx man!

Still there is one problem with this approach... And the problem is like you
said, when
element contains other stuff... then I'm not quite sure that first child
node is textnode.



I have huge code, and I prefer not to change it, which goes through elements
on page,
and reading/changing values according to some rules. Somethimes there is
posibility
that current control is not empty (label is actually DIV, and DIV is
container) becouse
those are dinamically generated controls in very complex asp.net
application.

Someone wrote this code last year and left the company, and I'm trying to
make it Mozilla
compatibile without making changes in concepts and algorithms.

So if u have any other idea besides firstChild.nodeValue I would be very
grateful.....
If not, I can try to use this with some aditional handling....
Thx anywway...

You can make a reasonable version of innerText using a regular
expression and innerHTML:

var re = /<[^<>]+>/g;
var iText = theDiv.innerHTML.replace(re,'');

If you have form controls with text content (e.g. textarea or option
elements) it will be best to not have them inside the label or the
above will return their text too (I don't know what innerText does
with that).

DOM 3 has 'textContent', but I don't think it's particularly well
supported just yet - v. 1.0 became a recommendation in April 2004.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top