Search code examples
javascripthtmlcsstextvar

Text before and after Var


I'm trying to put a text before and after the Var content. Unfortunately I was able to put the text only after. I'm a fan, so I don't know Js very well.

My content currently looks like this: 0.000 Kcal (this is an html placeholder)

When the function is executed, the content looks like this: x.xxx Kcal (this is the result of the function + text after var)

What I'm trying to do should be like this: BMR x.xxx Kcal (so add some text before the numbers, but it is not html)

Below is the code.

var bmr_mifflin_man = (10*weight) + (6.25*height) - (5*age) + 5;  
document.getElementById ('bmr_mifflin_man').value = bmr_mifflin_man.toLocaleString('it-IT', {maximumFractionDigits: 0}) + " Kcal";

Project: https://jsfiddle.net/snake93/4n9dgbfw/21/


Solution

  • You can introduce a new variable where you put the entire content of the string, before setting the value:

    var bmr_mifflin_man = (10*weight) + (6.25*height) - (5*age) + 5;  
    var newvar = "Some text here"+bmr_mifflin_man.toLocaleString('it-IT',{maximumFractionDigits: 0}) + " Kcal";
    document.getElementById ('bmr_mifflin_man').value = newvar