0

Happy New Years. I have a simple javascript math equation and I'm able to display the results fairly easy using HTML but I'd also like the results in a hidden Input field so that when the form gets submitted via email the variable is part of the email as well.

Here is my javascript code:

function updateScore(ele,val){
    var score = document.getElementById('score');
    var riskLevel = document.getElementById('risklevel');

    var curScore = parseFloat(score.innerHTML);
    curScore += (ele.checked ? val : -val);
    score.innerHTML = curScore;

    if(curScore <= 1){
        riskLevel.innerHTML = "Low";
        $("#scan_icon").html('<img src="img/RiskLow.jpg">');
    }else if(curScore <= 2){
        riskLevel.innerHTML = "Moderate";
        $("#scan_icon").html('<img src="img/RiskModerate.jpg">');
    }else if(curScore <= 4){
        riskLevel.innerHTML = "High";
        $("#scan_icon").html('<img src="img/RiskHigh.jpg">');
    }else{
        riskLevel.innerHTML = "Very High";
        $("#scan_icon").html('<img src="img/RiskVeryHigh.jpg">');
    }
}

How would I display 'score' in a hidden input? Tried several things but couldn't get it to work. This isn't working:

<input name="Risk Score: " input id="score" value="" type="hidden" />
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
Jeff S
  • 45
  • 4

2 Answers2

2

The simplest solution would be to ad something akin to

document.getElementById('score').value = curScore;

Using jQuery, you would do it this way:

$('#score').val(curScore);
Gary
  • 13,303
  • 18
  • 49
  • 71
ayemossum
  • 81
  • 5
0

You can store score in hidden input using jQuery as like that:

 $("#scorehidden").val(curScore);

And one more thing your HTML for hidden input should be like this:

<input name="RiskScore"  id="scorehidden" value="" type="hidden" />

Side note: you are already using score as a id in any div etc... so I changed the id name as scorehidden.

devpro
  • 16,184
  • 3
  • 27
  • 38