15

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(){ ... });
Ashwin
  • 12,081
  • 22
  • 83
  • 117

5 Answers5

20

You can use ^ selector.

Example

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

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

Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
5

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

$('.class').each(function(i,e) { // });
E. van der Spoel
  • 260
  • 1
  • 15
5
$('element[id^="ID"]').each(function () {
console.log(this.value);
});

Where element is the name of your targeted html element.

Talha Imam
  • 1,046
  • 1
  • 20
  • 22
2

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/

CristianD
  • 138
  • 1
  • 12
0
  function(ID)
  {
        ... $("#ID"+ID) ...
  }

  for (i=1;i<3;i++)
  {
       function(i);
  }
Richard
  • 455
  • 6
  • 15