Skip to content Skip to sidebar Skip to footer

Iterating Through All The Instances With Name Having A Common String

Is there a way to access all the object instances starting with a common string. Example: I have instances named button64, button223, button856471, button229846, etc. I have no con

Solution 1:

You can use regular expressions to find any expression matching the pattern. Using the match method, every instance of the pattern is returned as an array.

var str = document.getElementById("sample");
var arr = string.match(/button[\d]*/g);

The regular expression on line two will match any result that has "button" and will stop once it encounters a character afterwards that is not a digit.

Post a Comment for "Iterating Through All The Instances With Name Having A Common String"