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

182
Visualizações
How to use jQuery selectors with variables?

I have seen lots of similar questions, but none of answers worked in my case.

A variable is set depending on a document width:

if ($(document).width() < 1400) {
    var tblProdukty = document.getElementById('tblProdukty1280');
}
else {
    var tblProdukty = document.getElementById('tblProdukty1920');
}

Then I would like to do something like this: $("#tblProdukty1280 > tbody").empty(); but with a variable set as above into something like this:

$("#" + tblProdukty + " > tbody").empty();

or

$(`${tblProdukty}` + " > tbody").empty();

I have tried different combinations of concatenation and injection and could not figure it out. I got the following error message: Uncaught Error: Syntax error, unrecognized expression: #[object HTMLTableElement] > tbody

Can you please advice on this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can do it in 2 different ways:

first, you can use the tblProdukty variable as reference to the element:

if ($(document).width() < 1400) {
    var tblProdukty = $("#tblProdukty1280");
}
else {
    var tblProdukty = $("#tblProdukty1920"); 
}

Then use it this way:

tblProdukty.find("tbody").empty();

OR you can do something like that:

if ($(document).width() < 1400) {
    var tblProdukty = "tblProdukty1280";
}
else {
    var tblProdukty = "tblProdukty1920";
}

Then use it this way:

$(`#${tblProdukty}` + " > tbody").empty();
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