Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6545395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:34:51+00:00 2026-05-25T11:34:51+00:00

is there a way in jQuery to select elements that have a certain value

  • 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="person2" 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?

The solution should take in account that certain citynames appear in other city names (in example 2: London, New London)

Example 2:

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

What is the best way in jQuery to select all persons with “London” in the data-city attribute? A city with “New London” should not be selected.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:34:51+00:00Added an answer on May 25, 2026 at 11:34 am

    You can use the selector tag[attr*=string] where *= matches the string found anywhere in the tag value. I have colored the text red, just so you can test…

    $("li[data-city*=New York]").css({color:'red'});
    

    Or via more complex method to fit needs of example two:

    $("li")
        .filter( function(){ 
                return $(this).attr('data-city').match(/(^|,\s+)London(,|$)/) 
            })
        .css({color:'red'});
    

    This method uses filter to go through the list of selected li and match all elements with attribute data-city that matches regex (^|,\s+)London(,|$) which means…

    • start or comma (^|,)
    • and one or more spaces (\s+)
    • followed by London
    • followed by comma or end (,|$)

    I used this HTML:

    <li id="person1" data-city="Boston, New York, San Fransisco, London">
        Person name 1
    </li>
    <li id="person2" data-city="Boston, New Jersey, London, San Fransisco">
        Person name 2
    </li>
    <li id="person3" data-city="Los Angeles, New York, New London, Washington">
        Person name 3
    </li>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there a way in jQuery to select elements that have a certain value
Is there a better way to select grandparent elements in jQuery in order to
Using jQuery, is there a way to select all tag that reference a specific
Is there a way to filter a multi-line select box using jQuery? I'm a
In a jquery modal dialog, is there a way to select a button as
Using JQuery, is there a simple way to select the text immediately after a
How can I select all elements that have a specific CSS property applied, using
I have an SVG that I will add elements to. And on mousemove this
I have an array of strings that are valid jQuery selectors (i.e. IDs of
Is there a way in jQuery to know if an element already has an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.