take value from an elementbyid.innerhtml to calculate and place in html

Asked

Viewed 179 times

1

Oops, so... I have a value of an input type range that is being played in a span I want to take the value of that span with javascript, make a calculation with that span number and put in another span.

js code:

 var parcela = document.getElementById('exibePercent').value;
 var parcela1 = 4;
 var result1 = parcela / parcela1;
 document.getElementById("parcela1").value = result1;

html:

<span id="exibePercent">1000</span> <!----  SPAN QUE QUERO PEGAR O VALOR E JOGAR NO OUTRO SPAN----> 
<span id="parcela1" value=""></span> <!----- SPAN QUE QUERO QUE RETORNE O VALOR DO RESULT DO JS ---->

1 answer

2


To get the value of a span, you should not use the property value, but rather the innerHTML. Both to obtain the value and to exchange the same.

function trocarValorSpan() {
  var parcela = Number.parseInt(document.getElementById('exibePercent').innerHTML);
  var parcela1 = 4;
  var result1 = parcela / parcela1;
  document.getElementById("parcela1").innerHTML = result1;
}
span {
  display: block;
}

button {
  display: block;
}
<span id="exibePercent">1000</span> <!----  SPAN QUE QUERO PEGAR O VALOR E JOGAR NO OUTRO SPAN----> 
<span id="parcela1" value=""></span> <!----- SPAN QUE QUERO QUE RETORNE O VALOR DO RESULT DO JS ---->

<button onclick="trocarValorSpan()">Trocar valor da span</button>


var parcela = Number.parseInt(document.getElementById('exibePercent').innerHTML);
var parcela1 = 4;
var result1 = parcela / parcela1;
document.getElementById("parcela1").innerHTML = result1;
<span id="exibePercent">1000</span> <!----  SPAN QUE QUERO PEGAR O VALOR E JOGAR NO OUTRO SPAN----> 
<span id="parcela1" value=""></span> <!----- SPAN QUE QUERO QUE RETORNE O VALOR DO RESULT DO JS ---->

Documentation: https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML

  • I need the value of the new span to be automatically displayed, no button, as I can do?

  • Just leave the code "loose" and not within a function, I will create another example for you to see.

  • Another doubt I have is that the span "display" is dynamic, always change value, so the new span also has to be dynamic

  • I don’t know exactly what you want to do, but if you want the value to keep changing, you can use the function setInterval, if the value should change when something "something else" changes, it may be possible to use the event onchange.

  • I have an input range type, it’s a loan simulator. The user defines the value of his loan that is played for the current exibePercent, hence with this value of the input, I want to define the value of the installments, dynamically.

  • So you can do it using onchange, just create a function with the JS of the answer and put in the onchange where the user informs the value.

  • 1

    I got it, thank you.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.