1

I'm trying to declare a variable inside function and wants to make it available outside that function. I have gone through several Stackoverflow answers but still cant figure out.

script>
    (function() {
        var url
        $("#airline-select").change(function () {
            url = "{% url 'airline_year_financial_data' %}";
        });
        console.log(url)
    var httpRequest;
    makeRequest();

    // create and send an XHR request
    function makeRequest() {
        httpRequest = new XMLHttpRequest();
        httpRequest.onreadystatechange = responseMethod;
        httpRequest.open('GET', url)
        httpRequest.send()
    }
    // Handle XHR Response
    function responseMethod () {
        if (httpRequest.readyState === 4) {
            if (httpRequest.status === 200) {
                updateUISuccess(httpRequest.responseText)
                } else {
                    // Do something
                }
        }
    }
    // Handle XHR Success
    function updateUISuccess(responseText) {
        var response = JSON.parse(responseText)
        var a = $("#airline-select").val();

        var width = 500;
        var height = 300;
        var padding = 20;
        d3.select("body")
            .append("svg")
                .attr("class", "waterfall-container")
                .attr("width", width)
                .attr("height", height)
                .attr("style", "outline: thin solid")
        d3.select("svg")
            .selectAll("rect")
            .data(response)
            .enter()
            .append("rect")
                .attr("x-axis", 200)
                .attr("y-axis", 300)
                .attr("width", 20)
                .attr("height", 200)
                .attr("fill", "blue")
    }

})();

</script>

It logs 'undefined' at the console which means the value of url inside the function is getting updated or not available outside the function. How can I do that?

9
  • 2
    You're logging the value immediately after you attach the event handler, before the event has happened. Are you asking how you can make your variable predict the future? Commented Jul 30, 2017 at 4:18
  • What are you trying to achieve? Commented Jul 30, 2017 at 4:23
  • What is $('#airline-select') selecting? I suspect it is giving undefined because that element, which has an event listener (on change), has not yet been changed. In that case, because you declared var url (which is saying var url = undefined, logging url should correctly yield undefined Commented Jul 30, 2017 at 4:23
  • Thanks, I'm not really sure about fixing it. All I want is that, this url variable gets the new value and I can something with this url object outside the function. Commented Jul 30, 2017 at 4:23
  • What task needs to be performed outside of the event handler? Commented Jul 30, 2017 at 4:24

1 Answer 1

2

You are missing $ before immediately invoked function expression, if you are expecting jQuery() alias for .ready() to be called when document has loaded DOM and #airline-select is defined in document at HTML.

You can pass url to makeRequest() within change event handler by defining an expected parameter at the function declaration.

$(function() {

    var httpRequst, url;

    $("#airline-select").change(function () {
      url = "{% url 'airline_year_financial_data' %}";
      makeRequest(url)
    });

    // create and send an XHR request
    function makeRequest(url) { // include `url` parameter at function
        httpRequest = new XMLHttpRequest();
        httpRequest.onreadystatechange = responseMethod;
        httpRequest.open('GET', url)
        httpRequest.send()
    }
    //
})
Sign up to request clarification or add additional context in comments.

1 Comment

If you are expecting change event to be dispatched immediately you can chain .change() to $("#airline-select").change(function () { url = "{% url 'airline_year_financial_data' %}"; makeRequest(url) }).change()

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.