0

I need some help with the code below:

    <script type="text/javascript">
        var opcao = new Array (
            document.getElementById("cred_form_1864_1_wpcf-pde_Pesquisas_descontinuadas"),
            document.getElementById("cred_form_1864_1_wpcf-pnps_Pesquisas_nao_patenteaveis-sigilosas")
        );

        function check1(){
            if (opcao[0].checked == true){
                for (var i = 0; i < opcao.length; i++){
                    if (i != 0){
                        opcao[i].checked = false;
                        opcao[i].disabled = true;
                    }
                }
            }
        }

        function check2(){
            if (opcao[1].checked == true){
                for (var i = 0; i < opcao.length; i++){
                    if (i != 1){
                        opcao[i].checked = false;
                        opcao[i].disabled = true;
                    }
                }
            }
        }
    </script>

I need to call the array 'opcao' within the functions 'check1 ()' and 'check2 ()'. How can I do this?

====================

I tried the suggestion of Mishik did not work. What I need is that the functions 'check1 ()' and 'check2 ()' to access the same array 'opcao'. If I write the array within each function the code works perfectly. But how are the same values ​​I would have to repeat the same array for each function created.

6
  • 3
    What do you mean by "call"? Commented Jul 13, 2013 at 17:00
  • 1
    Why do you use such short id's ? are you mixing JS programming with typing exercise ? ;) Commented Jul 13, 2013 at 17:03
  • Why nou use radio-buttons? Commented Jul 13, 2013 at 17:12
  • A fiddle would be useful Commented Jul 13, 2013 at 17:20
  • 1
    Soon People will be explaining what the element is in their id 's like this <div id="header-banner-shows-visitors-of-latest-deals-in-september-this-year---bye"> <img src="..."/> </div> Commented Jul 13, 2013 at 17:41

1 Answer 1

2

If your code is executed before the page is loaded, then opcao array will not actually contain required elements.

You need to wrap your code in window.onload, so that by the time script is executed all the required elements will be available in the document.

window.onload = function() {
    var opcao = new Array (
        document.getElementById("cred_form_1864_1_wpcf-pde_Pesquisas_descontinuadas"),
        document.getElementById("cred_form_1864_1_wpcf-pnps_Pesquisas_nao_patenteaveis-sigilosas")
    );

    function check1(){
        if (opcao[0].checked == true){
            for (var i = 0; i < opcao.length; i++){
                if (i != 0){
                    opcao[i].checked = false;
                    opcao[i].disabled = true;
                }
            }
        }
    }

    function check2(){
        if (opcao[1].checked == true){
            for (var i = 0; i < opcao.length; i++){
                if (i != 1){
                    opcao[i].checked = false;
                    opcao[i].disabled = true;
                }
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I believe merely placing the script tag at the bottom would be sufficient in the OP's case. Nevertheless, this is a more fool-proof solution.

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.