Mike
Mike

Reputation: 735

Selecting element which starts with "abc" and ends with "xyz"

I have elements in my page with ids like "abc_1_2_3_xyz" .How do I select element in Jquery which starts with "abc" and ends with "xyz"?

$('div[id^="abc"], div[id$="xyz"]');

Upvotes: 2

Views: 807

Answers (3)

Andrew Clark
Andrew Clark

Reputation: 208405

Try the following:

$('div[id^="abc"][id$="xyz"]');

http://api.jquery.com/multiple-attribute-selector/

Upvotes: 4

KingKongFrog
KingKongFrog

Reputation: 14419

Use filter:

$('div')
    .filter(function() {
        return this.id.match(/^abc+xyz$/);
    })
    .html("Matched!")
;

Upvotes: 0

Ram
Ram

Reputation: 144659

You can use 2 attribute selectors.

$('div[id^="abc"][id$="xyz"]');

Upvotes: 5

Related Questions