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

247
Vistas
How to insert javascript variable in HTML div

I have this

<div id="chart1" class="bar-chart secondary" data-total="42" animated>
<span class="bar-chart--inner" style="width:42%;"></span>
<span class="bar-chart--text">42% </span>
</div>

and I have one javascript variable var score that I need to assign to data-total="42"and <span class="bar-chart--text">42% </span> My intention is to replace 42 with my javascript variable. I have tried this document.getElementById("chart1").innerHTML =score that I have found from this forum but did not work. Please help.

<div id="chart1" class="bar-chart secondary" data-total="document.getElementById("chart1").innerHTML =score" animated>
    <span class="bar-chart--inner" style="width:42%;"></span>
    <span class="bar-chart--text">42% </span>
    </div>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes

get the element in a JS script

then set elem.dataset.total


document.getElementById("chart1").dataset.total = score

<body>
  <div id="chart1" class="bar-chart secondary" data-total="42" animated>
    <span class="bar-chart--inner" style="width:42%;"></span>
    <span class="bar-chart--text">42% </span>
  </div>
  <script type="text/javascript">
    var score = 20

    document.getElementById("chart1").dataset.total = score
  </script>
</body>

For setting the html of an element, you can use innerHTML, just need to select that element in another lookup.


document.querySelector(".bar-chart--text").innerHTML = score

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