0

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

2
  • 2
    Instead of trying to replace anything, you should wrap the $150 inside the headline into an additional span element - then you can simply explicitly set the text content for that span to a new value, without having to know the old one to "replace." Commented May 9, 2022 at 12:59
  • 1
    Additional info: You also have to convert the number into an Integer. If not you will concat the new amount instead of calculation of this. For that exists parseInt(). Commented May 9, 2022 at 13:09

3 Answers 3

1

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>`;
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.