2

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
  • Do you mean to be passing runThisSecond as the callback to runThisFirst? If so, I think it should be like this: runThisFirst($(this), runThisSecond) Commented Dec 29, 2020 at 2:11

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

No problem, callbacks are magical until you realize you're just passing a function to another function as an argument!

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.