Hi,
This code generates 4 questions with 4 radio buttons and generates a message according to a calculated result. It’s working. No errors in the console. But there’s a problem with the middle message. If you choose the first buttons for each answer expect one, the message jumps to the high risk factor, when it’s not.
function getValueForOption(optionName){
var result;
var radios = document.getElementsByName(optionName);
for (i = 0; i < radios.length; i++) {
if (radios[i].checked){
result=radios[i].value;
return result;
}
}
}
window.onload = function() {
/* Wait until the html is loaded and then add a
submit event */
document.getElementById("risk-assessment").onsubmit = function(){
var age = getValueForOption("age");
var bmi = getValueForOption("bmi");
var genetics = getValueForOption("genetics");
var diet = getValueForOption("diet");
var diabetesRisk = age + bmi + genetics + diet;
var message;
var messageTitle = "Your results";
if( diabetesRisk <=15){
message = "Your results show that you currently have a low risk of developing diabetes." +
"However, it is important that you maintain a healthy lifestyle in terms of diet and exercise.";
} else if ( diabetesRisk <=25) {
message = "Your results show that you currently have a medium risk of developing diabetes." + riskFactorMessage +
"For more information on your risk factors, and what to do about them, please visit our diabetes website at http://www.zha.org.zd.";
} else {
var riskFactorMessage = "Your main risk factors are ";
var riskFactors = false;
if (age >=10 )
{
if (riskFactors== true) {
riskFactorMessage = riskFactorMessage + " and ";
}
riskFactorMessage = riskFactorMessage + " your <span>AGE<span> ";
riskFactors = true;
}
if (bmi >=10 )
{
if (riskFactors== true) {
riskFactorMessage = riskFactorMessage + " and ";
}
riskFactorMessage = riskFactorMessage + " your <span>BMI<span> ";
riskFactors = true;
}
if (genetics >= 10) {
if (riskFactors== true) {
riskFactorMessage = riskFactorMessage + " and ";
}
riskFactorMessage = riskFactorMessage + "your <span>GENETICS<span>";
riskFactors = true;
}
if (diet >= 10) {
if (riskFactors== true) {
riskFactorMessage = riskFactorMessage + " and ";
}
riskFactorMessage = riskFactorMessage + "your <span>DIET<span>";
riskFactors = true;
}
message = "Your results show that you currently have a HIGH risk of developing diabetes." + riskFactorMessage + ". We advise that you contact the Health Authority to discuss your risk factors as soon as you can." +
"Please fill in our contact form and a member of the Health Authority Diabetes Team will be in contact with you.";
}
document.getElementById("messageTitle").innerHTML = messageTitle;
document.getElementById("riskFactorAdvice").innerHTML= message;
return false;
}
}
May be someone can comment?
Thanks in advance.