I already saw this question however, it didn’t work for me whatever I tried.
My function is:
function validatePreviousChecked(parent, child) {
found = false;
element = document.getElementsByName(parent);
for (i = 0; i < element.length; i++) {
if (element[i].checked == true) {
found = true;
}
}
if (!found) {
alert("Please answer the questions in order. It seems that you didn't answer a previous question");
document.getElementsByName(child).checked = false;
return false;
}
return true;
}
function disableAll(elements) {
var myelements = document.getElementsByName(elements);
for ( var i = 0; i < myelements.length; i++) {
myelements[i].disabled = true;
}
return true;
}
and my radio button:
<input onclick="javascript: if(validatePreviousChecked('q1_financial_product','q1_product_likelihood[0]')){disableAll('q1_product_likelihood');}" value="1" name="q1_product_likelihood" type="radio" />
However, the first function is being executed, returning true (I validated it) but never the second one!
What is wrong?
EDITED
This is another snippet:
<input
value="1" name="q1_financial_product"
onclick="disableAll
('q1_financial_product')"
type="radio" />
There’s a lot wrong with the code. Don’t mean to bash it but here are some suggestions. The first is what I think is the cause of the problem, the others are just to improve your code.
"q1_product_likelihood[0]"as the child argument tovalidatePreviousCheckedwhich is used to calldocument.getElementsByName("q1_product_likelihood[0]"). That doesn’t make sense, the name of a control should not be q1_product_likelihood[0]. If there are multiple and you want the first one, you need to pass an additionalindexparameter that can be used to calldocument.getElementsByName(child)[index].ivariable in the loop for validatePreviousChecked is global, as isfound.javascript:That’s not wrong per se, since JS treats it as a label, but it’s not doing what you think it’s doing, remove that.disableAllreturn true? Doesn’t do any harm but adds bloat to the code and may make someone think that it means something.How did you validate that it’s returning true? If it’s returning true, it’s impossible that disableAll is not being called. You should prove that you validated it by specifying where you added log statements and what the output was.