-1

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" />
1
  • what is q1_financial_product? can you edit the question with html code? Commented Nov 17, 2011 at 7:00

1 Answer 1

1

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.

  • You're passing "q1_product_likelihood[0]" as the child argument to validatePreviousChecked which is used to call document.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 additional index parameter that can be used to call document.getElementsByName(child)[index].
  • Your i variable in the loop for validatePreviousChecked is global, as is found.
  • Your onclick handler starts with 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.
  • Instead of calling JS from the HTML, you should be setting handlers from the onload/domready handler, then you would have a regular, more readable function as the handler which you could debug, inline code is much harder to debug.
  • Why does disableAll return 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.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks. I added alert before the true so I could see that it gets there
did it you change what I suggested? did it work? You added an alert before true? There are two trues there. That's why I suggested you show the code you are using to validate. You should have a console.log('Returning true') before the return true; and you should add console.log('In disableAll') as the first line in disableAll. Better yet, you should just run a debugger and see the code path it's taking
How do I run a debugger for javascript? which one do you recommend?
Thanks! the problem was with sending the [index] inside. Can you refer me to a good tutorial of using handlers and debugger?
Sure, for starters, do not add JS code as html attributes. Beyond that, see my answer here stackoverflow.com/questions/4445759/… Each of the links there shows you how to debug in FF, Chrome and IE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.