Ashwin
Ashwin

Reputation: 12411

jquery for each all elements having similar id

I have many elements accross page - ID1, ID2 ID3 ...

I want to manipulate all the elements. Is there any simpler way to do this.

$("#ID").each(function(){ ... });

Upvotes: 15

Views: 47207

Answers (5)

Talha Imam
Talha Imam

Reputation: 1106

$('element[id^="ID"]').each(function () {
console.log(this.value);
});

Where element is the name of your targeted html element.

Upvotes: 5

Dipesh Parmar
Dipesh Parmar

Reputation: 27364

You can use ^ selector.

Example

$('div[id^="ID"]')

^= select DOM whose ID attribute starts with ID (i.e ID1, IDID,IDS,ID2 etc)

Upvotes: 21

CristianD
CristianD

Reputation: 138

If the ID portion isn't necessarily at the beginning you could do:

$( "[tagName][id*='ID']" )

Here's a full list of selectors: https://api.jquery.com/category/selectors/

Upvotes: 2

Richard
Richard

Reputation: 455

  function(ID)
  {
        ... $("#ID"+ID) ...
  }

  for (i=1;i<3;i++)
  {
       function(i);
  }

Upvotes: 0

E. van der Spoel
E. van der Spoel

Reputation: 270

Give them a class, so you can select them by the class?

$('.class').each(function(i,e) { // });

Upvotes: 4

Related Questions