Reputation: 6252
I have a button:
<button id="btn:1:uniquenamehere">btn</button>
the number in the middle sometimes changes due to other frameworks internal workings. I need to be able to find this button no matter the number in the middle. So I need to find it by pattern where only the middle number will change. So I need something like this:
document.getElementById("btn:*:uniquenamehere")
I saw ways of doing it using jquery, but I can't use 3rd party libraries. How can I do this with pure javascript?
Upvotes: 1
Views: 80
Reputation: 419
Also, if you'd like to fool around a bit, this is a good (if not as efficient) way:
function getButton (buttonid) {
var buttons = document.getElementsByTagName('button');
for (var i = 0; i < buttons.length; i += 1) {
if (buttons[i].id.slice(buttons[i].id.length - buttonid.length) === buttonid) {
return buttons[i];
}
}
return null;
}
Upvotes: 1
Reputation: 36794
You can use document.querySelectorAll()
:
var elems = document.querySelectorAll('[id$="uniquenamehere"]');
Upvotes: 3