user9588515
user9588515

Reputation:

Variable inside the function is not available in jquery but available without the function

I'm pretty new with javascript, but I'm really confused with a behavior of the function:

$('#get_file').on('click', function(event) {
    var start_date = $("#table_date_start").val(),
        end_date = $("#table_date_end").val(),
        href = apiUrl.concat(
                'download_pdf',
                '?start_date=' + start_date,
                '&end_date=' + end_date,
                '&order=', order[1],
                '&order_type=', order[0]
            );
    e.target.href = href;
});

variables start_date, end_date and href are not available inside the function, but available outside. How comes that? Could someone, please, give me a hint why is this happening?

Upvotes: 1

Views: 65

Answers (1)

Liftoff
Liftoff

Reputation: 25412

You are declaring all 3 vars in the same line, so they are not created until the entire line finishes. End the line declaring the first two vars before declaring the third one.

var start_date = $("#table_date_start").val();
var end_date = $("#table_date_end").val();

Upvotes: 5

Related Questions