0

So I want to check if there is any li element that has a attribute of "data-foo" and its value is "bar".

Would this work? :

if ($('li[data-foo=bar]'))
{
//exists
}
else
{
//does not exist
}

Thanks

eastboundr
  • 1,807
  • 8
  • 26
  • 44

1 Answers1

11

Get length of elements retured by selector if it is greater then zero then it means you have atleast one element with provided condition in selector.

if ($('li[data-foo=bar]').length > 0)
{
//exists
}
else
{
//does not exist
}
Adil
  • 143,427
  • 25
  • 201
  • 198