Re: Searching for text in nested table cell



Thanks for your help, Igor.

"Igor Tandetnik" <itandetnik@xxxxxxxx> wrote in message
news:ulOTEbTLHHA.448@xxxxxxxxxxxxxxxxxxxxxxx
"JWilliams" <ZZjohnZZwilliams_esq@xxxxxxxxxxxxx> wrote in message
news:...

"Igor Tandetnik" <itandetnik@xxxxxxxx> wrote in message
news:%233MQZ$dKHHA.4912@xxxxxxxxxxxxxxxxxxxxxxx
JWilliams <ZZjohnZZwilliams_esq@xxxxxxxxxxxxx> wrote:
In the searchTable subroutine above the required text is matched
using:
If InStr(TD.innerText, "target cell") Then

However, because the target cell is nested, simply looking at
TD.innerText is insufficient because it can contain the text of
cells lower down the DOM hierarchy, resulting in a spurious match
as shown by the debug line beginning 4 below:

You can check TD.all.length to look for a cell that doesn't have any
child elements.

That works nicely for the simplified example I posted, however the
real target cell has the search text within a <span> </span> tag,
which means that the cell has 1 child element. Looking for a cell
which has 1 child element and contains the text results in a couple
of spurious matches.

You can use TD.all.tags("td").length to check that the cell has no nested
<td> elements.

It's a pity you can't tell TD.innerText to only return text in the
immediate cell and not any nested cells.

You can use the DOM for that. Something along these lines:

const NODE_TEXT = 3;
var elem; // the element to get text content from
var children = elem.childNodes;
var text = "";
for (var i = 0; i < children.length; ++i) {
var child = children[i];
if (child.nodeType == NODE_TEXT) {
text += child.nodeValue;
}
}

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



.



Relevant Pages

  • Re: Searching for text in nested table cell
    ... If InStr(TD.innerText, "target cell") Then ... which means that the cell has 1 child element. ... var elem; // the element to get text content from ...
    (microsoft.public.inetsdk.programming.webbrowser_ctl)
  • Change the color of an "a" child element inside a cell ?
    ... I want to change the color of an "a" child element inside a table cell ... via javascript. ...
    (comp.lang.javascript)
  • RE: insert date
    ... If ActiveCell.Column = 4 Then 'Limits macro action to column D ... If ActiveCell.Value = "" Then 'Check to see if Target cell empty ...
    (microsoft.public.excel.worksheet.functions)
  • RE: insert date
    ... Private Sub Worksheet_SelectionChange ... If ActiveCell.Value = "" Then 'Check to see if Target cell empty ...
    (microsoft.public.excel.worksheet.functions)
  • RE: insert date
    ... That's a Change-event macro that goes in a Worksheet module, ... If ActiveCell.Value = "" Then 'Check to see if Target cell empty ...
    (microsoft.public.excel.worksheet.functions)

Loading