Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get siblings by text?

I can't seem to figure out how to get a sibling element by text. This is what I currently have:

var elem = $('div');
var e = elem.siblings('.condition').contains('text');
like image 613
rotaercz Avatar asked Dec 20 '25 08:12

rotaercz


2 Answers

jQuery has no contains() method which works as you expect it to. Instead you need to use the :contains selector. Try this:

var $elem = $('div');
var $e = elem.siblings('.condition:contains("text")');
console.log($e.length); // will be greater than 0 if an element was matched

You can also concatenate a variable in the selector if required:

var foo = 'bar';
var $e = $elem.siblings('.condition:contains("' + foo + '")');

And as suggested by @DavidThomas you could use filter():

var $e = $elem.siblings().filter(function() {
    return $(this).text() == 'text';
});
like image 114
Rory McCrossan Avatar answered Dec 21 '25 21:12

Rory McCrossan


the :contains selector should do everything you need:

var elem = $('div');
elem.siblings(".condition:contains('text')");
like image 26
Liam Avatar answered Dec 21 '25 20:12

Liam



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!