Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda