Reading value from label (DIV) with JS

Discussion in 'Javascript' started by Mateo, Aug 8, 2005.

  1. Mateo

    Mateo Guest

    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...
     
    Mateo, Aug 8, 2005
    #1
    1. Advertising

  2. Mateo

    Ivo Guest

    "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.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/
     
    Ivo, Aug 8, 2005
    #2
    1. Advertising

  3. Mateo

    Mateo Guest

    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...
     
    Mateo, Aug 9, 2005
    #3
  4. Mateo

    RobG Guest

    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
     
    RobG, Aug 9, 2005
    #4
  5. Mateo

    Mateo Guest

    Thx....

    Reg. exp. is pretty good idea....
     
    Mateo, Aug 9, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Josef K.

    <label><div></div></label> allowed?

    Josef K., May 20, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    13,658
    Josef K.
    May 22, 2005
  2. K Viltersten

    <div ... /> and <div ...></div>

    K Viltersten, Mar 30, 2009, in forum: ASP .Net
    Replies:
    4
    Views:
    787
  3. Dwayne Madsen

    Q: Div A inside Div B is larger than Div B

    Dwayne Madsen, Jun 1, 2005, in forum: Javascript
    Replies:
    1
    Views:
    217
    David Dorward
    Jun 1, 2005
  4. mscir
    Replies:
    3
    Views:
    342
    Martin Honnen
    Jun 26, 2005
  5. Knut Krueger
    Replies:
    8
    Views:
    306
    Knut Krueger
    Aug 20, 2013
Loading...

Share This Page