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

212
Visualizações
Jquery append div and dynamically set the width

I'm trying to append a div to a td in a table. However, as I am appending the div, I want to first take the clientWidth of the td (to take into account different screensize) and then set the width of the div to be that width:

var tdWidth = document.getElementById("someId").clientWidth; // ex: 165

td.append("<div Div1 style='margin:auto; width:150px;'><div msg style='color:red'>" + "Text, Text, Text" + "</div></div>");

Now I'm trying to apply the 165 value to Div1 (where the width is currently hardcoded to 150px - I'd want to not hardocode it but dynamically set it), but I have no clue how to do this, I tried to do things like:

  $('Div1').width(tdWidth);

and

  var tdWidthPx= tdWidth + 'px';

  $('Div1).css("width",tdWithPx);

Ideally I would want to set the width as I append it so that when the text id displayed it is displayed in the correct width.

Thanks in advance.

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

0

There is no HTML attribute called Div1 or msg. Hence, this becomes invalid HTML.

You can append the width like this without hardcoding it initially.

var tdWidth = document.getElementById("someId").clientWidth; // ex: 165

td.append("<div style='margin:auto; width:" + tdWidth + "px;'><div  style='color:red'>" + "Text, Text, Text" + "</div></div>");

Or else, you can assign an id and later change the width based on id.

var tdWidth = document.getElementById("someId").clientWidth; // ex: 165

td.append("<div id='Div1' style='margin:auto; width:150px;'><div style='color:red'>" + "Text, Text, Text" + "</div></div>"); // giving an id to the div element(this id should be unique)

The change the width using the id.

var tdWidthPx= tdWidth + 'px';

$('#Div1').css("width",tdWithPx);

Use the first method if you are not changing the div width dynamically later in the program.

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