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?

1

1 Answer 1

1

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

$('[data-city*="New York"]')
Sign up to request clarification or add additional context in comments.

2 Comments

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: See it now, thanks. And no, *= is the attribute contains selector: api.jquery.com/attribute-contains-selector

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.