How To get values in Span Tag from JS

前端 未结 6 1535
滥情空心
滥情空心 2021-01-15 19:38

I am trying to make a BMI Calculator using HTML. The inputs come from text fields. I am making some calculation with them in a Javascript function. I want to get the calcula

6条回答
  •  感动是毒
    2021-01-15 19:57

    If you have a variable like calcResult, you can do:

    document.getElementById('spanId').innerHTML += calcResult;
    

    Of course, that only works once. If you do it again, you'll have two results in the span. If you want to be able to change the result if the form fields change, I'd try:

    Your calculation result = 
    
    var result = 12;
    document.getElementById('calc').getElementsByTagName('span')[0].innerHTML = result;
    

提交回复
热议问题