Andrew
Andrew

Reputation: 15377

Select Only Text From Inside a div

I have the following html:

<td>
<div>
Text I Want<span class="note-comment">Text I Do Not Want</span>
</div>
</td>

This HTML is not mine and therefore cannot be changed. Yes, I know it would be ideal to wrap the "Text I Want" in a span, but its not mine.

Using CSS Selectors, I need to get "Text I Want", without selecting the span node as well. I have tried :first-child and was unsuccessful.

If it is absolutely impossible to do with CSS selectors, is there a way to do it it with XPath (Using text() I assume)?

Upvotes: 2

Views: 4873

Answers (2)

gastrodia
gastrodia

Reputation: 11

impossible in css but it's easy in program.

such as in javascript:

text.split('<')[0]

Upvotes: 0

Ry-
Ry-

Reputation: 225105

It is absolutely impossible to do with CSS selectors. CSS selectors rarely select text (exceptions being the ::first-line and ::first-letter pseudo-elements).

Why not select the <div> and get its firstChild? Anyways, the XPath would be something like //td/div/text()[1].

Upvotes: 2

Related Questions