0

With the Sizzle selector engine, is it possible to find buttons with the exact text?

For example, button:contains('Remove Document') will match any buttons that have the text "Remove Document" but I need it to not match buttons that say "Don't Remove Document". Is there a selector that will match the whole text, start to finish?

mpen
  • 272,448
  • 266
  • 850
  • 1,236

2 Answers2

2

You need to use filter there :

$("button").filter(function(){
    return $(this).text().trim()==='Remove Document';
});

If you really want to have it in one sizzle selector string, starting from adeneo's answer and completing it, you may use

button:contains("Remove Document"):not(:contains("Don"))
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Sorry, I only put the jQuery tag there in case people weren't familiar with Sizzle. I'm not using jQuery. I need a single selector string. – mpen Sep 06 '13 at 20:41
  • 1
    Using a single selector string here is doable (see adeneo's answer) but you might not be using the right tool, this will be uselessly heavy. – Denys Séguret Sep 06 '13 at 20:43
  • 1
    Darn, I was gonna say you could functionify this for more general use. http://jsfiddle.net/j08691/g3L3d/ – j08691 Sep 06 '13 at 20:45
1

You can put :contains inside :not to not match something :

$("button:not(:contains(Don't Remove Document))")

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • 1
    True, but that implies advocating an inordinately complex solution, if there are a number of buttons that contain, but are not limited to, the desired text. – David Thomas Sep 06 '13 at 20:42
  • @DavidThomas: We could always do `button:contains(Remove Document):not(:contains(Don't))` http://jsfiddle.net/mnbayazit/8guAm/1/ Which works for my scenario, but it's not as pretty as, say, `button[@text='Remove Document']` – mpen Sep 06 '13 at 21:06