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');
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';
});
the :contains selector should do everything you need:
var elem = $('div');
elem.siblings(".condition:contains('text')");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With