Check if item is in jQuery set

I have an element and a jQuery set. I would like to check if an element is inside a jQuery set. I need a condition like this.

if ($someUL.find("> li > a").contains(myElement)) {
  ...
}

The method containsdoes not exist here, I used it to demonstrate my purpose. I know a few techniques like this, for example has, is, $.containsbut they all have different meanings.

+4
source share
2 answers

Here you can use the method .index(). If the item is not found in the set, .index()will return -1 .:

if($someUL.find("> li > a").index(myElement) > -1){
   //myelement exists in set $someUL.find("> li > a")
}
+2
source

You can get your answer by reading the jQuery API .

, - , . .is().

API:

  • .has() Promise
  • .contain() $.contain()
  • .is() , , .

.is() API

+2

Source: https://habr.com/ru/post/1649266/


All Articles