Accessing variable from different function

后端 未结 2 1759
清歌不尽
清歌不尽 2021-01-29 06:49

I have the following code in HTML which creates a button, that when clicked creates some questions in my page, works fine.



        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-29 07:47

    According to How to declare a global variable in a .js file you can define a variable before any function is called and then use it in different functions.

    var global1 = "I'm a global!";
    
    function setGlobal () {
        global1= document.getElementById('num_questions').value;
    }
    function testGlobal () {
        alert(global1);
    }
    

提交回复
热议问题