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

187
Visualizações
How to add div with dynamic id with JavaScript

I am trying to create divs with Javascript:

var v = e[c].day;
        var m = g(new Date(t, n - 1, v))
          ? '<div id=`${v}/${v}/${y}` class="today" type="button" onclick="addRow()">'
          : '<div type="button" onclick="addRow()">';
        l.append(m + "" + '<span class="month">' + i[n-1]+ "</span>" + "" + v + "</div>");

So what I am trying to do is giving the id to the divs with the date and when I try to do like this id=${v}/${v}/${y} it is being written exactly like this: ${v}/${v}/${y} instead the date. So apparently, t is a year, n is a month and v is a day. So how can I give them an id like this ${v}/${v}/${y}?

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

0

Surround the whole string with backticks:

`<div id="${v}/${v}/${y}" class="today" type="button" onclick="addRow()">`

See What are valid values for the id attribute in HTML?.

about 4 years ago · Juan Pablo Isaza Relatório

0

In the first output of your ternary statement it looks like you're returning the string using single quotes instead of backticks.

I suspect backticks is what you need:

var v = e[c].day;
        var m = g(new Date(t, n - 1, v))
          ? `<div id="${v}/${v}/${y}" class="today" type="button" onclick="addRow()">`
          : '<div type="button" onclick="addRow()">';
        l.append(m + "" + '<span class="month">' + i[n-1]+ "</span>" + "" + v + "</div>");
about 4 years ago · Juan Pablo Isaza Relatório

0

div is a closing tag that's why you must close every div in your code

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