7

I'm new to JQuery please help. I need to select multiple elements knowing only part of Id: eg:

<div id="element32422455">Some content</div>
<div id="element68475124">Some content</div>
<div id="element42352355">Some content</div>

Is is possible to select those elements using staff like this

$('#element*');

If you know any other good way to do it, please tell.

swyx
  • 46
  • 4
Iegor Benzyk
  • 103
  • 1
  • 1
  • 8
  • You can use the 'attribute starts with' selector: `$('[id^=element]')` Note however that it would be much quicker (and better practice) to give all those elements a common class and select by that, `$('.element')` – Rory McCrossan Mar 22 '16 at 19:54

1 Answers1

11

You can do it by using attribute starts with selector,

var elems = $('[id^=element]');

There is no need to use wild card selector at this context, it is actually called attribute contains selector. And that will select the element though it has id like "123213element12312"

DEMO

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130