10

I'm pretty sure this can be done without going into a function but I want to grab any TR where there is a <td>Mail ...</td> or <td>Foo ... </td> as examples.

I keep thinking it should look like

$('tr[./text()^="foo"]') but chrome doesn't like it. or $('tr[./td/text()^="Mail"]') but no luck can this be done with a simple selector?

Maslow
  • 18,464
  • 20
  • 106
  • 193
  • 1
    Selectors have to be valid CSS selectors (more or less). But they are not XPath! You cannot do this with a simple selector, but maybe with `filter`. – Felix Kling Mar 06 '11 at 00:38

3 Answers3

24

I'm quite surprised no one came up with this, maybe my question was too vague.

$('tr:has(td:contains("Leather"))').hide()
$('tr:has(td:contains("Mail"))').hide()
$('tr:has(td:contains("Cloth"))').hide()
$('tr:has(td:contains("Bow"))').hide()
$('tr:has(td:contains("Gun"))').hide()

Going to make it a bookmarklet for searching for armor and weapons for a dk or pally

Maslow
  • 18,464
  • 20
  • 106
  • 193
  • 2
    Well, as stated in the answers, `:contains` searches for text in any part of the element, while you asked specifically for start with. Also, you can string selectors together with a comma (`,`) so that you won't have to use 5 `$` and `hide()`s – Yi Jiang Mar 06 '11 at 02:50
  • neat, I was talking about from a :has and overall total solution answer, rather than just pieces of the solution. – Maslow Mar 06 '11 at 03:07
  • No, your question was too specific. – BoltClock Mar 06 '11 at 03:16
  • For reference a good look at the , option Yi Jiang is talking about : http://stackoverflow.com/questions/2416803/jquery-contains-selector-to-search-for-multiple-strings/2417076#2417076 – Maslow Mar 06 '11 at 03:25
  • @BoltClock - well no where in the question does it specify starts with, that's just the xpath operators I was trying to use – Maslow Mar 06 '11 at 03:33
  • 1
    @Maslow: The title of your question is *Select any TR where a TD's text **starts** with foo*. And fact is that you cannot do this with a simple selector. As you have the only information about the text, yes, you could have had a look whether the words you are looking for are also contained in the other cells or not.... anyway, at least you have your solution. – Felix Kling Mar 06 '11 at 09:24
  • @Felix Kling - ah tunnel vision at the question instead of the title. – Maslow Mar 06 '11 at 18:29
  • Your solution worked perfectly for me: `$('tr:has(td:contains("StatusID:1"))').addClass('CompletedRow');` – HerrimanCoder May 05 '15 at 13:51
5

One possible solution:

$('td').filter(function() {
    return $(this).text().indexOf('Foo') === 0;
}).closest('tr');

You have to use .filter() to work on the element's text (you can't do this with a selector, at least not the way you want to) and .closest() should be self explaining.

Edit: But this could select a <tr> elements multiple times. I actually don't know how jQuery handles this. Another solution would be to use two nested filters (on tr and on td but somehow this feels inefficient).

Edit2: From version 1.4.4 on, jQuery seems to be smart enough to deal with multiple occurances of the same element (i.e. it works like a set). DEMO here (if you change to version 1.4.2, you'll see that the first row gets selected twice).

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • looks like wowhead.com is `This page already using jQuery v1.4.2` so I'll have to deal with the duplication somehow. – Maslow Mar 06 '11 at 01:27
  • was starts with was a premature optimization on my part? it may not be worth the tradeoff in readability loss to optimize string search – Maslow Mar 06 '11 at 01:51
1

There is "contains:"

TD:contains("Mail")

But that just looks for Mail anywhere in the element (there is no 'starts with').

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
  • this may be a great fit based on the tiny scale of my usage. I would like to know ways to accomplish for larger scale or more scalable options if any exist though. going to try this one. – Maslow Mar 06 '11 at 01:55
  • `$('td:contains("Mail")::ancestor/[td]').hide()` syntax issue but getting very close – Maslow Mar 06 '11 at 02:03
  • http://api.jquery.com/attribute-starts-with-selector/ is a starts with but it's only for attributes... is the text somehow able to be treated as an attribute? – Maslow Mar 06 '11 at 02:04