You cannot achieve this using getElementById . The best solution would be querySelector or querySelectorAll , where you get full support for CSS selectors.
Link in MDN
You will need these two attribute selectors:
Get items that start with a specific string
document.querySelector('[id^="stack_"]');
Get items that contain a specific string
document.querySelector('[id*="stack_"]');
Get items that complete with a specific string
document.querySelector('[id$="_overflow"]');
By combining the final and starting selectors, you get the following and you can achieve the desired result:
document.querySelector('[id^="stack_"][id$="_overflow"]');
Happy coding!
source share