user1547174 user1547174 - 7 months ago 13
Javascript Question

How to pass data between jquery click events

I have a select change click event that captures data that I need to be passed to another click event. Is there a way I can do this? Here's my pseudo-code:

Select change event:

$(".item_status").change(function() {

//I want to pass data stored in my order_status variable to my save_info click event below
var order_status = $('option:selected',this).text();


//now open a modal
$("#my_modal").modal('toggle');

});

$("#save_info").click(function() {
//This save_info button is inside of my my_modal modal
//I want to use the order_status variable defined in the change event above:
console.log(order_status);

});

GG. GG.
Answer

Declare your variable outside of the function.

var order_status;

$(".item_status").change(function() {

    order_status = $('option:selected',this).text();

    $("#my_modal").modal('toggle');

});

$("#save_info").click(function() {

    if (!order_status) return;

    console.log(order_status);

});