1

Possible Duplicate:
Find all elements on a page whose element ID contains a certain text using jQuery

I need to select all elements have "user" word in their ID.

in attribute I will do it by $= but how can I do it by element ID?

Community
  • 1
  • 1
Moein Hosseini
  • 4,309
  • 15
  • 68
  • 106

4 Answers4

3

You'd do it with the use of the *= selector:

$('[id*="user"]').doStuff();
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
3

You want something like this

$('[id*="user"]')

selects all elements

[id with an id

*="user" that contains user

Example: http://jsfiddle.net/jasongennaro/QXGen/2/

Here is more info on the attribute contains selector: http://api.jquery.com/attribute-contains-selector/

Jason Gennaro
  • 34,535
  • 8
  • 65
  • 86
  • 1
    the leading `*` is redundant. All CSS selectors are parsed from right-to-left, meaning the attribute selector is interpreted first, so that `*` will just slow the selector down after it gets all the elements it needs. – Chris Sep 03 '11 at 13:49
  • Thanks @Chris. Noted and edited above. – Jason Gennaro Sep 03 '11 at 13:53
2

An attribute selector like $('[id*="user"]') will match elements with the id 'user', 'users', 'superuser', 'abusers', 'user-details', etc.

If you had a more rigid structure for your IDs and wanted to only match things like 'user' and 'super-user' etc., you could try something like:

var userWordTest = RegExp("\\b" + "user" + "\\b");
$('[id]').filter(function() { return userWordTest.test(this.id); })
Chris
  • 9,994
  • 3
  • 29
  • 31
2

Use the contains variant of the named attribute selector with the id attribute.

$('[id*="user"]')...
tvanfosson
  • 524,688
  • 99
  • 697
  • 795