0

I want to get a reference to an elment which has exactly a certain text value with jquery css selector.

Example:

<table >
 <tbody>
  <tr>
   <td>
    <i>MYVAL</i>  <!-- I want the reference to this element-->
   </td>
  </tr>
 </tbody>
</table>

But I cannot succeed in it just writing

$('table tbody tr td i[value="MYVAL"]').SomeFunction();

Which is the correct syntax?

Daniele B
  • 3,117
  • 2
  • 23
  • 46
  • If you're asking a jQuery question, don't title-tag it with CSS please. – BoltClock Jan 07 '13 at 15:17
  • 2
    The term for the text "MYVAL" is not `value`, it's `content`. The term "value" is used for `` elements (etc) that actually have a "value" attribute. – Pointy Jan 07 '13 at 15:18
  • but this is aso about css selectors... – Daniele B Jan 07 '13 at 15:18
  • @DanieleB It's primarily a jQuery question, and you generally shouldn't include tags in the title of your questions anyway. Questions already *have* tags to specify what the question relates to. – Anthony Grist Jan 07 '13 at 15:20
  • 1
    You cannot select an element by its exact text content with either a CSS selector or a jQuery selector (not even with `:contains()`). You're going to have to check its text manually. – BoltClock Jan 07 '13 at 15:21
  • There is no exact text selector in jQuery due to whitespace ambiguities, see http://stackoverflow.com/questions/6673777/select-link-by-text-exact-match for alternatives – Alex K. Jan 07 '13 at 15:22
  • @AnthonyGrist ok I removed the css tag – Daniele B Jan 07 '13 at 15:26

3 Answers3

4

NOTE thats not using pure CSS selector!

Using .filter():

http://jsfiddle.net/p6fKe/

$('table tbody tr td i').filter(function(){return $(this).text() == "MYVAL"}).SomeFunction();
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
2

Use this code

$('table tr td i:contains("MYVAL")').SomeFunction()

DEMO: http://jsfiddle.net/enve/6wjDj/

Idrizi.A
  • 9,819
  • 11
  • 47
  • 88
  • 3
    That's going to match any `` element that contains `MYVAL` in its content, even if its content is not *exactly equal* to `MYVAL`. I'm not sure this will satisfy the requirements of the question. – Anthony Grist Jan 07 '13 at 15:22
1
function selectTextElement(text) {
    var elements = [];
    $('table tbody tr td i').each(function() {
        if (jQuery(this).text() == text) {
            elements.push(jQuery(this));
        }
    });
    return elements;
}
algorhythm
  • 8,530
  • 3
  • 35
  • 47