16

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?

Liam
  • 27,717
  • 28
  • 128
  • 190
mousesports
  • 509
  • 1
  • 6
  • 18
  • You should add a class like `font` to all such divs and select by class name. Your approach is really bad practice. – dfsq Dec 02 '12 at 15:47
  • I'm aware and I normally would. But I'm using Options Framework for WordPress and it doesn't allow for this - each option has its own id. – mousesports Dec 02 '12 at 15:53

5 Answers5

23

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:

James Allardice
  • 164,175
  • 21
  • 332
  • 312
1

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]);
Adil
  • 146,340
  • 25
  • 209
  • 204
1

Try this:-

 var elements = $("[id$='_font']");
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

Yes it is possible, use:

var elements = $('[id$=_font]');
Gurpreet Singh
  • 20,907
  • 5
  • 44
  • 60
0

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

$("div[id$='_font']")
Kyle Burton
  • 26,788
  • 9
  • 50
  • 60