How to return variable value from jQuery event function?

You cannot return variable value from jQuery event function. To understand the reason, let us see how data is passed, which was created in the event handler of a button click.

The main issue is that event functions in jQuery are asynchronous, meaning they execute after the main code flow has completed. When you try to return a value from an event handler, there's nothing to receive that return value since the calling function has already finished executing.

Understanding the Problem

Event handlers work differently from regular functions because they are callback functions that get executed when an event occurs, not when they are defined. This asynchronous nature means any return statement inside an event handler won't be captured by the code that set up the event listener.

Alternative Approaches

Instead of trying to return values directly, you can use these methods ?

Example 1: Using Custom Events

Here's how to pass data using custom events ?

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            var a = document.getElementById('demo');
            a.addEventListener('click', function(){
                var s = 'Hello World!';
                var event = new CustomEvent('build', { 'detail': s });
                this.dispatchEvent(event);
            });

            document.getElementById('parent').addEventListener('build', getData, true);

            function getData(e){
                alert(e.detail);
            }
        });
    </script>
</head>
<body>
    <div id='parent'>
        <button id='demo'>Click me</button>
    </div>
</body>
</html>

Example 2: Using Global Variables

A simpler approach using global variables ?

var globalData;

$(document).ready(function(){
    $('#myButton').click(function(){
        globalData = 'Data from event handler';
        console.log('Data stored globally:', globalData);
    });
    
    $('#getDataButton').click(function(){
        if(globalData) {
            alert('Retrieved data: ' + globalData);
        } else {
            alert('No data available yet');
        }
    });
});

Example 3: Using Callback Functions

The most elegant solution using callback functions ?

function handleButtonClick(callback) {
    $('#myButton').click(function(){
        var data = 'Hello from event handler!';
        callback(data);
    });
}

$(document).ready(function(){
    handleButtonClick(function(data){
        console.log('Received data:', data);
        alert(data);
    });
});

Conclusion

While you cannot directly return values from jQuery event functions due to their asynchronous nature, you can work around this limitation using custom events, global variables, or callback functions to pass data between event handlers and other parts of your code.

Updated on: 2026-03-13T19:08:02+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements