How to pass multiple #id to javascript function

Coreysan 1,811 Reputation points
2022-06-03T00:55:13.61+00:00

I have this:

const c1 = document.getElementById("input1").addEventListener("change", h1, false);

function h1() { $.each($('#input1')[0].files, function (key, input) { name1 = input.name; formData.append("file1", input, name1); }); }

Is it possible to loop through several id elements and call one function?

const c1 = document.getElementById("input1").addEventListener("change", GetFile, false);
const c2 = document.getElementById("input2").addEventListener("change", GetFile, false);

Pseudocode:

foreach (var id in id_elements)
{
     function GetFile() { $.each($('???????????????')[0].files, function (key, input) { name1 = input.name; formData.append("file1", input, name1); }); }
}
Developer technologies ASP.NET ASP.NET Core
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.