Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery find exception with contains selector

I have some code running with jQuery 1.7.2 I encountered an error when trying to find using this selector

var x = "span:contains(\"C) Foo (Bar)\")";
$('body').find(x)

https://jsfiddle.net/elewinso/dfbn82wo/7/

This issue is fixed in JQuery 1.8 and up but since I cannot switch my jQuery I was hoping to find a patch.

like image 570
elewinso Avatar asked Jan 01 '26 10:01

elewinso


1 Answers

The problem is your selector is confusing jQuery's parser because it contains brackets. It thinks the ) you are trying to match is the closing bracket of its contains: content filter.

You can get around this by using filter() instead.

$('span').filter(function() {
    return $(this).text().indexOf('"C) Foo (Bar)"') != -1;
});
like image 189
Mitya Avatar answered Jan 06 '26 13:01

Mitya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!