0

I have a function that allows only 1 checkbox to be ticked at a time. I need to access a value stored in that checkbox outside of this function. For example, in function test below:

JavaScript code:

        function OnlyOne(checkbox) {
            var checkboxes = document.getElementsByName('checkbox_answer')
            
            checkboxes.forEach((item) => {
                if (item !== checkbox) item.checked = false
                else{
                    var checkbox_id = checkbox.value;
                    alert (checkbox_id); // defined and working fine
                    
                }
            })
            alert("test1");
            alert ("test2"+checkbox_id); //checkbox_id is not defined
        }
       function test{
           alert (checkbox_id); //checkbox_id is not defined
        }

I have tried adding in HTML body:

<p id="pageid"></p>

and then calling in <script>:

var variabletest = document.getElementById("pageid").innerHTML =checkbox_id;
Sunil Kumar Das
  • 372
  • 1
  • 2
  • 12
LuQchor
  • 13
  • 4

0 Answers0