mousesports
mousesports

Reputation: 499

Find all elements by part of the id attribute

I was wondering if this was possible:

I have a set of divs, each with an ID ending in '_font', e.g 'body_font', 'heading_font', 'tagline_font', etc.

Is there a way to grab those elements by searching for a common portion of their names, in this case '_font' so I can later manipulate them using jQuery?

Upvotes: 16

Views: 22002

Answers (5)

Adil
Adil

Reputation: 148180

You can use wild cards, $ will give you elements which have ending with give string, learn more about ends with selctor here.

elements = $('[id$=_font]);

Upvotes: 1

Kyle Burton
Kyle Burton

Reputation: 27588

jQuery's Attribute Ends With Selector should do what you are asking:

$("div[id$='_font']")

Upvotes: 0

Gurpreet Singh
Gurpreet Singh

Reputation: 21233

Yes it is possible, use:

var elements = $('[id$=_font]');

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172628

Try this:-

 var elements = $("[id$='_font']");

Upvotes: 1

James Allardice
James Allardice

Reputation: 166071

You can use an "attribute ends-with" selector:

var elems = $("div[id$='_font']");

If you spend some time browsing the jQuery API you should be able to answer questions like this yourself without having to post on Stack Overflow.

Other selectors that might come in useful:

Upvotes: 23

Related Questions