[IE vs Gecko & DOM] Getting text from a <div> in a <tr>

Discussion in 'Javascript' started by F. Da Costa, Dec 12, 2003.

  1. F. Da Costa

    F. Da Costa Guest

    Hi,

    I' looking to retrieve ProdName1 form the <tr> below.

    <tr id="1-1-1" class="even">
    <td>
    <div class="tier4">
    <a href="#" class="leaf"></a>
    ProdName1
    </div>
    </td>
    <td><input type="checkbox" name="checkbox$2" checked="1" onClick="setCheckbox(this);"/></td>
    <td><input type="text" name="text2Edit$2" value="100" align="right"/></td>
    </tr>

    In Gecko it works as follows.
    var vals = r.getElementsByTagName("div");
    obj = vals[0];
    _searchValues[_searchValues.length]=obj.textContent;

    obj.textContent retrieves ProdName1 without any problem in *Gecko*

    Obviously I also need to do this with IE and obviously the above does *not* work.
    Does anybody has a suggestion as to how this can be accomplished using IE?

    TIA,
    Fermin DCG
    F. Da Costa, Dec 12, 2003
    #1
    1. Advertising

  2. On Fri, 12 Dec 2003 10:04:57 +0100, F. Da Costa <> wrote:
    > Hi,
    >
    > I' looking to retrieve ProdName1 form the <tr> below.
    >
    ><tr id="1-1-1" class="even">
    > <td>
    > <div class="tier4">
    > <a href="#" class="leaf"></a>
    > ProdName1
    > </div>
    > </td>
    > <td><input type="checkbox" name="checkbox$2" checked="1" onClick="setCheckbox(this);"/></td>
    > <td><input type="text" name="text2Edit$2" value="100" align="right"/></td>
    ></tr>
    >
    > In Gecko it works as follows.
    > var vals = r.getElementsByTagName("div");
    > obj = vals[0];
    > _searchValues[_searchValues.length]=obj.textContent;
    >
    > obj.textContent retrieves ProdName1 without any problem in *Gecko*


    Well you could get the tr element using getElementById and then walking
    down the tree IF you can guarantee that structure. Myself I might well
    put an id on the <div> and doing a getElementById on that.

    --
    Andy Leighton =>
    "The Lord is my shepherd, but we still lost the sheep dog trials"
    - Robert Rankin, _They Came And Ate Us_
    Andy Leighton, Dec 12, 2003
    #2
    1. Advertising

  3. F. Da Costa

    F. Da Costa Guest

    Andy Leighton wrote:
    > On Fri, 12 Dec 2003 10:04:57 +0100, F. Da Costa <> wrote:
    >
    >>Hi,
    >>
    >>I' looking to retrieve ProdName1 form the <tr> below.
    >>
    >><tr id="1-1-1" class="even">
    >> <td>
    >> <div class="tier4">
    >> <a href="#" class="leaf"></a>
    >> ProdName1
    >> </div>
    >> </td>
    >> <td><input type="checkbox" name="checkbox$2" checked="1" onClick="setCheckbox(this);"/></td>
    >> <td><input type="text" name="text2Edit$2" value="100" align="right"/></td>
    >></tr>
    >>
    >>In Gecko it works as follows.
    >>var vals = r.getElementsByTagName("div");
    >>obj = vals[0];
    >>_searchValues[_searchValues.length]=obj.textContent;
    >>
    >>obj.textContent retrieves ProdName1 without any problem in *Gecko*

    >
    >
    > Well you could get the tr element using getElementById and then walking
    > down the tree IF you can guarantee that structure. Myself I might well
    > put an id on the <div> and doing a getElementById on that.

    obj already contains the <div> so that is not the problem.
    The trick lies in getting hold of the text *without* the <a> coming along (like in using childNodes[0])
    Even IE 6 gives me an undefined whilst trying to get to 'textContent'
    F. Da Costa, Dec 12, 2003
    #3
  4. "F. Da Costa" <> writes:

    > obj already contains the <div> so that is not the problem.
    > The trick lies in getting hold of the text *without* the <a> coming along (like in using childNodes[0])


    Addressing child nodes by fixed offset numbers should be
    avoided. Elements won't have the same offset in different browsers (IE
    doesn't include all-whitespace text nodes in the DOM tree, Mozilla does).

    > Even IE 6 gives me an undefined whilst trying to get to 'textContent'


    The property "textContent" is part of the comming W3C DOM 3
    specification, which very few browsers support (IE still struggles
    with, e.g., DOM 2 Events). It might be equivalent to the IE property
    "innerText", but that won't help you in other browsers. This should work
    in any W3C DOM compliant browser:

    function getTextContent(node) {
    if (node.nodeType == 3) {return node.nodeValue;} // text node
    if (node.nodeType == 1) { // element node
    var text = [];
    for (var chld = node.firstChild;chld;chld=chld.nextSibling) {
    text.push(getTextContent(chld));
    }
    return text.join("");
    }
    return ""; // some other node, won't contain text nodes.
    }

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Dec 12, 2003
    #4
  5. F. Da Costa

    F. Da Costa Guest

    Lasse Reichstein Nielsen wrote:
    > "F. Da Costa" <> writes:
    >
    >
    >>obj already contains the <div> so that is not the problem.
    >>The trick lies in getting hold of the text *without* the <a> coming along (like in using childNodes[0])

    >
    >
    > Addressing child nodes by fixed offset numbers should be
    > avoided. Elements won't have the same offset in different browsers (IE
    > doesn't include all-whitespace text nodes in the DOM tree, Mozilla does).
    >
    >
    >>Even IE 6 gives me an undefined whilst trying to get to 'textContent'

    >
    >
    > The property "textContent" is part of the comming W3C DOM 3
    > specification, which very few browsers support (IE still struggles
    > with, e.g., DOM 2 Events). It might be equivalent to the IE property
    > "innerText", but that won't help you in other browsers. This should work
    > in any W3C DOM compliant browser:
    >

    Thx for the snip, much appreciated.

    Had run into the nodeType as well but wasn't to sure what to do whith them.
    Would it be save to assume that their definitions are described somewhere on the w3c?
    What, in your your opinion, is the most reliable source for obtaining the things like the nodeType definitions,
    functions pertaining to standard objects/ the DOM model etc?

    > function getTextContent(node) {
    > if (node.nodeType == 3) {return node.nodeValue;} // text node
    > if (node.nodeType == 1) { // element node
    > var text = [];
    > for (var chld = node.firstChild;chld;chld=chld.nextSibling) {
    > text.push(getTextContent(chld));
    > }
    > return text.join("");
    > }
    > return ""; // some other node, won't contain text nodes.
    > }
    >
    > /L

    TIA
    Fermin DCG
    F. Da Costa, Dec 13, 2003
    #5
  6. "F. Da Costa" <> writes:

    > Had run into the nodeType as well but wasn't to sure what to do
    > whith them. Would it be save to assume that their definitions are
    > described somewhere on the w3c?


    Yep: <URL:http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-1950641247>

    > What, in your your opinion, is the most reliable source for obtaining
    > the things like the nodeType definitions, functions pertaining to
    > standard objects/ the DOM model etc?


    The W3C DOM specifications are available on their web page. I have links
    to them here:
    <URL:http://www.infimum.dk/HTML/references.html#ref_1_5>

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Dec 13, 2003
    #6
  7. F. Da Costa

    F. Da Costa Guest

    Lasse Reichstein Nielsen wrote:
    > "F. Da Costa" <> writes:
    >
    >
    >>Had run into the nodeType as well but wasn't to sure what to do
    >>whith them. Would it be save to assume that their definitions are
    >>described somewhere on the w3c?

    >
    >
    > Yep: <URL:http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-1950641247>
    >
    >>What, in your your opinion, is the most reliable source for obtaining
    >>the things like the nodeType definitions, functions pertaining to
    >>standard objects/ the DOM model etc?

    >
    >
    > The W3C DOM specifications are available on their web page. I have links
    > to them here:
    > <URL:http://www.infimum.dk/HTML/references.html#ref_1_5>

    Thx, exactly what i was looking for.
    Much appreciated.

    >
    > /L

    DCG
    F. Da Costa, Dec 14, 2003
    #7
    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. K Viltersten

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

    K Viltersten, Mar 30, 2009, in forum: ASP .Net
    Replies:
    4
    Views:
    738
  2. F. Da Costa

    [DOM related] IE vs. Gecko

    F. Da Costa, Dec 11, 2003, in forum: Javascript
    Replies:
    1
    Views:
    104
    F. Da Costa
    Dec 11, 2003
  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:
    182
    David Dorward
    Jun 1, 2005
  4. Luke Matuszewski

    Gecko DOM window onclose event ?

    Luke Matuszewski, Dec 3, 2005, in forum: Javascript
    Replies:
    6
    Views:
    209
    Thomas 'PointedEars' Lahn
    Dec 4, 2005
  5. david.karr
    Replies:
    4
    Views:
    212
Loading...

Share This Page