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,857
  • 8
  • 28
  • 46

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
  • 146,340
  • 25
  • 209
  • 204