If you mean you could have 20 different attributes where you have Type and Value in your question, yes, you can readily do that work in a loop:
["id", "name"].forEach(function(prop) {
$html.find('[' + prop + '*=PLACEHOLDER]').attr(prop, function() {
var match = /PLACEHOLDER\.([A-Z]+)/i.exec(this[prop]);
if (match) {
this[prop] = "filters" + len + "." + match[1];
}
});
});
That looks for all elements with PLACEHOLDER in their id or name and updates the id or name with the replacement, taking the attribute name ("Type" or "Value", etc.) from the id or name.
Live Example:
var len = 27;
var $html = $("#template").clone();
["id", "name"].forEach(function(prop) {
$html.find('[' + prop + '*=PLACEHOLDER]').attr(prop, function() {
var match = /PLACEHOLDER\.([A-Z]+)/i.exec(this[prop]);
if (match) {
this[prop] = "filters" + len + "." + match[1];
}
});
});
console.log($html.html());
<div id="template">
<input id="PLACEHOLDER.Type">
<input name="PLACEHOLDER.Type">
<input id="PLACEHOLDER.Value">
<input name="PLACEHOLDER.Value">
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
It's simpler if we can assume the id and name are on the same element:
$html.find('[id*=PLACEHOLDER]').attr("id", function() {
var match = /PLACEHOLDER\.([A-Z]+)/i.exec(this.id);
if (match) {
this.id = this.name = "filters" + len + "." + match[1];
}
});
That looks for all elements with PLACEHOLDER in their id or name and updates the id or name with the replacement, taking the attribute name ("Type" or "Value", etc.) from the id or name.
Live Example:
var len = 27;
var $html = $("#template").clone();
$html.find('[id*=PLACEHOLDER]').attr("id", function() {
var match = /PLACEHOLDER\.([A-Z]+)/i.exec(this.id);
if (match) {
this.id = this.name = "filters" + len + "." + match[1];
}
});
console.log($html.html());
<div id="template">
<input id="PLACEHOLDER.Type">
<input name="PLACEHOLDER.Type">
<input id="PLACEHOLDER.Value">
<input name="PLACEHOLDER.Value">
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
.find. However, in each line only thePLACEHOLDERis replaced. Thus, it could be replaced by a single line always replacingPLACEHOLDERwith a value independent of the current attribute.idon those elements and only need class. Also suspect that using classes would make this all a lot simpler both in targeting the elements as well as updating the attributes. Show an html sample