SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist stonedeft's Avatar
    Join Date
    Aug 2009
    Posts
    590
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jquery select all elements in regex

    Hello forums

    In JQuery how do you select all elements using regex.
    I have this markup

    Code HTML4Strict:
    <span id="td_12345">something</span>
    <span id="td_12343">something</span>
    <span id="td_123345">something</span>
    <span id="td_1255">something</span>
    How do I select all id's starting with td_

    Thanks.
    Don't Panic

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    I think you need the attribute-contains-selector: Attribute Contains Selector [name*="value"] – jQuery API

    Code:
    $('span[id*="td_"]')

  3. #3
    SitePoint Evangelist stonedeft's Avatar
    Join Date
    Aug 2009
    Posts
    590
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow thats easier than a regex solution. Thanks
    Don't Panic

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by stonedeft View Post
    Wow thats easier than a regex solution. Thanks
    If you want to ensure that it doesn't accidentally match with something in the middle of the name, you can use the attribute-starts-with selector instead:

    $('span[id^="td_"]')

    It also makes things easier for you or others who come back to the code, because you can tell immediately what the code is expected to achieve.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Ooh, even better! Hadn't seen that one. jQuery is amazing (hope DS60 doesn't read this )


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •