Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
Replace selector with array item

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

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

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()))
)
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!