Jeff S Jeff S - 4 years ago 210
HTML Question

input type hidden from javascript variable

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" />

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download