0

I looked all around, but couldn't find any help with this problem.

So I have this if that is calling a function. In that I am waiting for a button press to happen. The button, therefore, should return true or false, but it's only returning to its anon-function running inside the click event and I don't know how to return from its parent function.

if ( saveConfirm() ) {
    saveFunction()
}

function saveConfirm () {
  $('#confirm').click(function() {
    $('modal').modal('hide')
    return true
  })
  $('#abort').click(function() {
    $('modal').modal('hide')
    return false
  })
}

Hope you guys understand what i mean and maybe someone can help me with how to return from the call to the if through the button that is pressed.

Siyual
  • 16,415
  • 8
  • 44
  • 58
  • $('#confirm').click(function() - this line is binding click event to the element with id=confirm – kleinohad Dec 02 '15 at 13:53
  • put `;` after `$('modal').modal('hide')`. – Ajay Makwana Dec 02 '15 at 13:55
  • Yeah, i know that. I am just wondering if there is still a way to return from the parent/binding function or if it is impossible in any way. – Maria Wilkov Dec 02 '15 at 13:55
  • @AjayMakwana that's not really helpful. I'm using js-standard in which you do not set any ' ; ' – Maria Wilkov Dec 02 '15 at 13:59
  • What is js-standard? If you're talking about vanilla JS then you should know that while you don't necessarily need to end lines with semi-colons because of ASI, ASI is merely [an _error correcting procedure_](https://brendaneich.com/2012/04/the-infernal-semicolon/). – Andy Dec 02 '15 at 14:07
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Whymarrh Dec 02 '15 at 14:10
  • `saveConfirm()` is undefined since nothing is returned from it. undefined is falsey so the code will not execute `saveFunction()` also you should note that you are returning the values from the click events. You should also follow the other advice to have the saveFunction called in the confirm click event – synthet1c Dec 02 '15 at 14:45

2 Answers2

3

Click handlers execute asynchronously, you should move saveFunction call to confirm click handler. There is no way for saveConfirm return boolean value you are trying to return in click handler.

  $('#confirm').click(function() {
    $('modal').modal('hide');
    saveFunction();
  })
  $('#abort').click(function() {
    $('modal').modal('hide');
  })
0

You bind events in saveConfirm(). Try code below

  $('#confirm').click(function() {
    $('modal').modal('hide');
    saveFunction();
  })
  $('#abort').click(function() {
    $('modal').modal('hide');
  })
Konstantin Zadiran
  • 1,485
  • 2
  • 22
  • 35