Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through all IDs that begin with XXX

Would anyone know how to loop through all ID's that being with name_

So, for example, within the markup I may have 50 id's that all start with "name_", the full ID would be like name_2, name_55, name_25, etc.

I'd like to loop through all of these getting the number.

Not really sure where to begin....... thank you!

like image 233
dzm Avatar asked Aug 31 '10 04:08

dzm


People also ask

Can you loop through an object JavaScript?

Object. key(). It returns the values of all properties in the object as an array. You can then loop through the values array by using any of the array looping methods.

How do I iterate over an object key?

The Object. keys() method was introduced in ES6. It takes the object that you want to iterate over as an argument and returns an array containing all properties names (or keys). You can then use any of the array looping methods, such as forEach(), to iterate through the array and retrieve the value of each property.


1 Answers

use the attribute starts with selector

$('[id^=name_]').each(function() {
    var number = this.id.split('_').pop();
});
like image 88
Anurag Avatar answered Oct 14 '22 06:10

Anurag