0

How I convert local variable to global variable in JavaScript?

like

window.addEventListener("load", function () {

    var fakeRadioButtonsFranType = document.querySelectorAll(".iCheck-helper");
    for (var i = 0; i < fakeRadioButtonsFranType.length; i++) {
        fakeRadioButtonsFranType[i].addEventListener("click", function () {
            fran_type = Number(this.parentNode.querySelector("input").value);

            var dataPass = 'fran_type=' + fran_type;
                $.ajax({ // Send the username val to available.php
                    type: 'POST',
                    data: dataPass,
                    url: '<?= site_url('Alluser/getLocations'); ?>',
                    success: function (responseText) { // Get the result

                       var allZonals = responseText;
                    }
                });
         });
      }
});   

I want use allZonals variable .

5
  • declare into window object. Commented Aug 6, 2016 at 11:48
  • window.allZonals = allZonals. Or don't declare it as a var but just use window.allZonals Commented Aug 6, 2016 at 11:48
  • assign to window object or create in global scope, and just modify Commented Aug 6, 2016 at 11:48
  • As I understand your problem you want to access response outside ajax. Commented Aug 6, 2016 at 11:50
  • Hint: your other variable fran_type is already global... Commented Aug 6, 2016 at 11:56

1 Answer 1

1

You can assign it to global window object:

window.allZonals = responseText;
Sign up to request clarification or add additional context in comments.

Comments

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.