0

I've written some jQuery code like this:

$('#checkbox').click(function() {
    // do stuff
});

function myFunction() {
    $('#checkbox').click();
}

The desired behavior here is for the checkbox click handler to be invoked when myFunction executes. However, the behavior I observe is that the click handler runs and the checkbox is toggled. How can I run the click handler without toggling the checkbox?

3
  • Try using .change(). Commented Apr 29, 2013 at 18:59
  • @DanielAllenLangdon, there are a lot of suggested answers that are assuming different things. Perhaps you could clarify exactly what you want to happen. Commented Apr 29, 2013 at 19:10
  • I believe that the question is clear: I want to run the code in the checkbox's click handler without toggling the checkbox. Commented Apr 29, 2013 at 19:28

4 Answers 4

2
$('#checkbox').triggerHandler("click");

Or make your click handler a function that you can call directly.

Sign up to request clarification or add additional context in comments.

2 Comments

It works because it does not fire an actual event, it only calls the handlers. The doc fyi
Thanks for the answer. I learnt a little bit reviewing the relevant jQuery documentation: api.jquery.com/triggerHandler
0

You should invoke a method when the checkbox is changed so you can call the same method outside of the click event:

$('#checkbox').change(function() {
    someMethod();
});

function myFunction(){
    someMethod();
}

Comments

0

How about you do the processing in the function and have the click handler invoke the function:

function myFunction() {
    // do stuff
}

$('#checkbox').click(function() {
  myFunction()
});

Comments

0

Try like this:

$('#checkbox').click(function(event) {
    event.preventDefault();
    // do stuff
});

See working demo


Another option to differentiate when called from myFunction() or not:

$('#checkbox').click(function(event, caller) {
    if (caller == 'MF') {
        event.preventDefault();
    }
    // do stuff
    $(this).parent().append('<span>It wasn\'t checked</span><br>');
});

function myFunction() {
    $('#checkbox').trigger('click', ['MF']);
}

$('#clk').on('click', myFunction );

See working demo

6 Comments

What happens when a user clicks the checkbox and acutally want it to toggle ?
lots of answers but this is the only one that even appears to be trying to solve the issue. @adeneo, OP specifically said he doesn't want it to toggle (for whatever reason)
@smerny - The OP specifically said he did'nt want it to toggle when the event is triggered from another function, not always ?
@adeneo Just call (or not) .preventDefault() conditionally then, but the OP has not specify what that condition would be..
The condition would be to not check the checkbox when the event handler is triggered from another function. How do you intend to solve that conditionally ?
|

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.