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

158
Views
Pass selector into javascript function

i have a js function that insert some code into exsiting html. HTML

<div id="filteredProducts">
    <!--load by js-->
</div>

<div id="filteredplaces">
    <!--load by js-->
</div>

<div class="container">
    <div class="bucket">
    </div>
</div>

js code

var myfun = function (data) {
    if (data.form_is_valid) {
        $("#filtered1").html(data.html);
        toggleLight();
    } 

I need further abstract it to take another paramter so that i can insert data.html into any selector. So "#filtered1" will become a parameter pass into this function. For example (Not sure if it works)

var myfun = function (data, sel) {
    if (data.form_is_valid) {
        $(sel).html(data.html);
        toggleLight();
    }

Ideally i'd like to do following:

myfun(data,".container .bucket")

or

myfun(data,"#filteredplaces")
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

const myFunction = (data, selector) => {
    if (data.isFormValid) {
        $(selector).html(data.html);
        // toggleLight();
    }
};
myFunction({ isFormValid: true, html: '<h1>Hello</h1>'}, '.class1,.class2')

This seems to work as you intended. I just tested with the last line, and it does what you need.

Perhaps, you missed that you need a comma in between the classes when you are targeting multiple

about 4 years ago · Juan Pablo Isaza 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!