By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,200 Members | 1,617 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,200 IT Pros & Developers. It's quick & easy.

Reading value from label (DIV) with JS

P: n/a
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...

Aug 8 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Ivo
"Mateo" wrote

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.nodeVa lue;
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.nodeVa lue = 'Some new text';

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

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



Aug 8 '05 #2

P: n/a
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...
Aug 8 '05 #3

P: n/a
Mateo wrote:
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.

--
Rob
Aug 9 '05 #4

P: n/a
Thx....

Reg. exp. is pretty good idea....
Aug 9 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.