1

I want to get a div that contains 'xxx' in its innerHTML.

I could achieve that with the following code :

$("div:contains('xxx')")

Unfortunately, I can't use this because another plugin already provided me the div.

Can I do something like this?!

element.contains('xxx')

I know I could use filter, but I want to keep it simple.

Can these pseudo-selectors be used as a property with the dot notation?

For example, if I want the last element in the selectors I could use this:

 $("div:last")

Is there something like this below?

$("div").last() 

I know I could use the code below but I want to keep it simple:

$("div").eq($("div").length-1)

Any idea how these pseudo-selectors can be used using the dot notation (as properties of the selected elements)?

4
  • But I cant use this cause another plugin already provided me the "div" can you explain what you mean by this? If the element's in the DOM then you can select it however you like. Commented Sep 25, 2015 at 15:23
  • I am working inside another plugin that gets the elements from an external plugin. I have access only to "element" and I need to use it. I mean, I really need to use "element" in the selector. Commented Sep 25, 2015 at 15:25
  • one of JetBrain recommendation is split your "simple pseudo" selector to separate function call. Don`t try to make it harder to read... Commented Sep 25, 2015 at 15:26
  • Is it an actual DOM element that you have access to or the jQuery object? Commented Sep 25, 2015 at 15:28

3 Answers 3

1

What you can do is select the given div like so:

div.find(":contains(xxx)")

Provided div is a jQuery object otherwise use $(div).

After that just check if the selection contains any elements. The same goes for the last selector:

$("div").find(":last")
Sign up to request clarification or add additional context in comments.

Comments

0

Any idea how these "pseudo" selectors can be used using the dot notation (as properties of the selected elements)?

Some of them have indeed a method equivalent. .last exists. But for those that don't you'd have to use .filter. Here is a list of all jQuery methods where you can look for yourself.

Comments

0

I think .is() may be what you are looking for. Try

$(element).is(":contains('xxx')");

for DOM Element or

element.is(":contains('xxx')");

for jQuery object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.