0

is there a way in jQuery to select elements that have a certain value in a data attribute array?

Consider this snippet of html:

<li id="person1" data-city="Boston, New York, San Fransisco">
    Person name 1
</li>
<li id="person1" data-city="Los Angeles, New York, Washington">
    Person name 2
</li>

What is the best way in jQuery to select all persons with "New York" in the data-city attribute?

murze
  • 4,015
  • 8
  • 43
  • 70
  • duplicate of http://stackoverflow.com/questions/4191386/jquery-how-to-find-an-element-based-on-a-data-attribute-value, which has a more thorough answer – drzaus Jul 26 '12 at 16:03

1 Answers1

1

even though i don't see your html, it should be:

$('[data-city*="New York"]')
Andy
  • 29,707
  • 9
  • 41
  • 58
  • i've corrected the html-snippet, you should be able to see it now. I think your solution only works if there was only one city in the data-city attribute – murze Sep 08 '11 at 07:13
  • 2
    @murze: See it now, thanks. And no, `*=` is the attribute contains selector: http://api.jquery.com/attribute-contains-selector/ – Andy Sep 08 '11 at 07:18