JSFiddle - React, Tailwind, and code Playground

by Matt Ball

HTML

<input id="test1foo" />
<input id="test2foo" />
<input id="testbar" />

JavaScript

RegExp.quote = function(str) {
     return str.replace(/([.?*+^$[\]\\(){}-])/g, "\\$1");
}; // from http://stackoverflow.com/questions/494035/#494122

String.prototype.endsWith = function(suffix) {
    return !!this.match(new RegExp(RegExp.quote(suffix) + '$'));
};

function getInputsThatEndWith(text) {
    
    var results = [],
        inputs = document.getElementsByTagName("input"),
        numInputs = inputs.length,
        input;
    
    for(var i=0; i < numInputs; i++) {
        var input = inputs[i];
        if(input.id.endsWith(text)) results.push(input);
    }
    
    return results;
}

console.log(getInputsThatEndWith("foo"));