2

i have define variable globally in java script and want to use it in google chart, but it giving undefined error.

  var period;
$(document).ready(function () {

 $('#period').on('change', function () {
    window.period = this.value;
    alert(period);
   });
    // period variable in google chart is showing undefined
      google.charts.load('current', {
    callback: function drawChart() {
     var jsonData = $.ajax({
            type: 'GET',
            url: "{{ url('/datesalesbarchartdata') }}/" + period ,
            dataType: "json",
            global: false,
            async: false,
            success: function (jsondata) {

            }
        }).responseText;
6
  • 3
    Might the whole code be inside some other IIFE? Try assigning to just period, or refer to window.period inside drawChart (also, are you sure the chart starts being loaded after period is first defined?) Commented Nov 16, 2018 at 7:03
  • Remove the var from in front of 'period' Commented Nov 16, 2018 at 7:11
  • 1
    @Difster Noooo! Commented Nov 16, 2018 at 7:12
  • Sure @Teemu - Check it out: jsfiddle.net/difster/ybku12q9/1 Commented Nov 16, 2018 at 7:17
  • @CertainPerformance it works when i insert drawChart in period below i will share working code.Thanks You all for your help. Commented Nov 16, 2018 at 7:37

1 Answer 1

1

Working code

     var period;
    $('#period').on('change', function () {
    window.period = this.value;

google.charts.load('current', {
    callback: function drawChart() {
        console.log(period);
       var jsonData = $.ajax({
            type: 'GET',
            url: "{{ url('/datesalesbarchartdata') }}/" + period ,
            dataType: "json",
            global: false,
            async: false,
            success: function (jsondata) {

            }
        }).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.