Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

160
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda