0

This seems like a very basic thing to do, but I don't understand why is not working: jsfiddle

function click(){
    $('#button').click(function(){
    return true;
  });
}

if(click()){alert('clicked')}

I want to return true if the button is clicked, is it possible to use the function directly into the if statement and then do something depending on the returned value ?

In the real scenario I need a function that can check for what button is clicked and depending on that I want to call other functions

if(click() == 'returned value'){differentFunction()}
Uiot
  • 83
  • 8
  • 2
    You can't, at the point your doing the check the click has not yet happened.. – Keith Jan 02 '19 at 17:09
  • The function you pass to `$('#button').click()` (and which you return from) is not the function you defined it inside (`function click()`) which is what you are testing the return value of. – Quentin Jan 02 '19 at 17:12
  • 3
    I don't think the answer that is shown as a duplicate is correct. This isn't an async call, it is about an event trigger. Similar, but different enough it shouldn't be marked a duplicate with that question. @Quentin – samanime Jan 02 '19 at 17:12
  • 1
    The function click will always return false. when the click handler is activated by you clicking the button the context of the function has been removed - the return true does not affect the if statement - which will have already happened on load anyway – Trujllo Jan 02 '19 at 17:13
  • Regarding your edit... This is logic you want to put inside the click handler, not surrounding the creation of the click handler. "What button is clicked" could be accomplished by using different click handlers for different buttons, or by checking some value within the single common click handler (such as a data attribute on the buttons). – David Jan 02 '19 at 17:16
  • I was binding the click() method on multiple buttons `$('#btn1','#btn2')` and then checking for what id was clicked `if(this.id == 'btn1')(return value)` – Uiot Jan 02 '19 at 17:20
  • @Uiot If you are binding to multiple buttons, and inside you are checking the id to determine which logic you need to do, meaning the logic for the buttons is not the same, then **don't bind to both buttons at the same time**. Bind to them individually so you do not have to interrogate them, and just do whatever logic each one needs. – Taplar Jan 02 '19 at 17:47
  • @samanime — The function passed to the click method is resolved *later* after the function which passes it there has resolved. That's what async is. – Quentin Jan 02 '19 at 19:38
  • @Quentin Yes, but for someone who is asking this type of question, the one you linked won't make sense in the context of the question that is marked duplicate. To beginners, those two questions are completely different. – samanime Jan 03 '19 at 12:32

2 Answers2

1

No.

Your function click() sets a handler. It says, at a future time, when someone clicks '#button', I want you to do a thing. Actually no "thing" has been set up because all the handler does is return. But it cannot return to your if because that will have long gone.

Perhaps put the alert inside the handler?

if is not when.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
-1

I think you are confused over what the click() method does.

The click() method is a trigger, which when clicked will perform some action. It can't return a value like you want. All you need is this:

$('#button').click(function () {
  alert('clicked');
});

If the button is clicked, that code will run. If it isn't clicked, it won't.

If you want it to only happen the first time it was clicked, you can use once():

$('#button').once('click', function() { alert('clicked') });

which will only ever fire once.

samanime
  • 25,408
  • 15
  • 90
  • 139