Pass a button element into callback function?

Viewed 57

I'm trying to pass a button element to the second function upon completion of first function. I've stepped through it with the debugger tool and the ele button being passed into the callback function is being passed correctly. However, the button element being passed in runThisSecond is returning undefined. Any ideas as to why that might be?

HTML file:


<button type="button" class="btn fruitbtn" data-fruitname= "@item.FruitName">   Add Fruit </button>

JavaScript:


$("$table#Results").on('click', '.fruitbtn', function() {
  runThisSecond(runThisFirst($(this)));
})
    
function runThisFirst (ele, callback) {
        swal({ 
            html:true,
            title: "Fruits"
        },
            function(isConfirm) {
                callback(ele);    //button element is correctly passed in here
            }
        )
    }
    
    function runThisSecond(ele) {
        var button = ele;    //This is returning undefined
    }
1 Answers

The only thing you're currently passing to runThisSecond is the value returned from runThisFirst, which is nothing (undefined). If you want runThisSecond to act as the callback parameter in runThisFirst, you'll need to pass it as the second argument:

runThisFirst($(this), runThisSecond)
Related