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

121
Vistas
Replacing a string value using the click event

I'm building a donate modal.

When I click on the donate button, the progress bar gets incremented based on the input value.

I want to replace the money string from h1 when I click on donate. So if the input value is 10, when I click on the donate button the h1 should read:

$140 is still needed for this project

This runs only once, because it replaces the text only when it finds '$150'.

.modal
      .modal-header-box
        h1 $150 is still needed for this project
      .modal-content
        progress#myProgress(value='50', max='200')
        h1 Only days left to fund this project. Join the other 42 other donors who have already suppoorted this project. Every dollar helps.
        input(type='number', placeholder='$' id='value', max='100')
        button#btn Give Now
      .button.save-later Save for later
      .button.social Tell your friends

JavaScript

document.getElementById('btn').addEventListener('click', function() {
  var x = document.getElementById('myProgress');

  console.log(x.value, document.getElementById('value').value)
  x.value = +x.value + +document.getElementById('value').value;

  let difference = x.max - x.value;
  console.log(difference);

  document.body.innerHTML = document.body.innerHTML.replace('$150', `${difference}`);

});

Is there any way to make this more dynamic, so the string will update If click on the donate button multiple times?

CodePen link

https://codepen.io/make96/pen/XWZdbPZ

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Your problem resides in replacing everything on the page. You should restrict the transformation to the elements that you are changing. Instead of replacing document.body, then target the modal header box instead.

document.body.innerHTML = document.body.innerHTML.replace('$150', `${difference}`);

Replace the modal-header-box content instead.

 document.getElementsByClassName("modal-header-box")[0].innerHTML = `<h1>$${difference} is still needed for this project</h1>`;
about 4 years ago · Juan Pablo Isaza Denunciar

0

Don't replace the document body, Not good practice to update the body every time, update only the required field.

Javascript

document.getElementById("btn").addEventListener("click", function() {
  const title = document.getElementById("title");
  const x = document.getElementById("myProgress");
  const prev = +x.value
  x.value = prev + +document.getElementById("value").value;
  let difference = x.max - x.value;
  title.innerHTML = title.innerText.replace(x.max - prev, `${difference}`);
});

Template

h1#title $150 is still needed for this project
about 4 years ago · Juan Pablo Isaza Denunciar

0

Just put the number in a span and only change the span input on every click. You take the value of the span and the value of the input box and substract it everytime you click on it.

Function:

document.getElementById("btn").onclick = function(){
  var value = parseInt(document.getElementById("test").innerHTML);

  var donate = parseInt(document.getElementById("value").value);

  document.getElementById("myProgress").value += donate;

  document.getElementById("test").innerHTML = value-donate;
}

modal-header-box

.modal-header-box
        h1 $<span id="test">150</span> is still needed for this project
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