I would like to archive that I have an Array, with 3-4 Items and then I want to itterate throw all of them and change the selector in the function with the items of the array.
let items = ['Name','Person,'New']
then my function
$('.btn').on("click", function(){do smth});
and then the foreach .btn should be like name or person, and I want maybe to add late more Items to the array. How should I do it?
for 3 functions
$('.name .btn').on("click", function()){do smth.})
$('.name .test').on("click", function()){do smth.})
$('.name .dosmth').on("click", function()){do smth.})
Name should be replaced with item of the array
I want unique function for the elements
Advertisement
Answer
Try this:
let items = ['name', 'person', 'new'] let elements = [{ name: 'btn', callback: () => { // do something } }, { name: 'test', callback: () => { // do something } }, { name: 'dosmth', callback: () => { // do something } } ] items.forEach(item => elements.forEach(element => $(`.${item} .${element.name}`).on("click", element.callback())) )