select some element by ID in jQuery [duplicate]
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?
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/
You'd do it with the use of the *=
selector:
$('[id*="user"]').doStuff();
Use the contains variant of the named attribute selector with the id attribute.
$('[id*="user"]')...
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); })
精彩评论