0

Basically I have code which is follows:

$('selector').on('click',function(){
    //Do something here
});
$('selector').click(function(){
    //Does exact same thing.    
});

Is it possible to combine these? Or would I simply make a function to call from inside both of these bindings?

Apologies if this has been answered, I did check the suggested questions, but didn't find any answers.

Nagra
  • 1,529
  • 3
  • 15
  • 25
  • Why do you even have both `.on('click')` and `.click()`? Don't they do the same thing? – JJJ Apr 19 '12 at 09:37
  • This should help you: http://stackoverflow.com/questions/4944293/jquery-liveclick-vs-click – SOLDIER-OF-FORTUNE Apr 19 '12 at 09:38
  • 1
    The .on function is for dynamic elements added onto the page. The click function is for elements which are initially loaded with the page. – Nagra Apr 19 '12 at 09:44
  • 1
    You can just use `on()` for both. There's no need to separate dynamic events from those already on the page. – JJJ Apr 19 '12 at 09:46
  • 1
    why the downvotes? it might be more of a beginner grade question but we all started somewhere. if you live in an ivory tower it pays to know what it's built on. – totallyNotLizards Apr 19 '12 at 09:47
  • Thanks @Juhana - my bad for asking such a silly question! – Nagra Apr 19 '12 at 09:53

4 Answers4

1

$('selector').click() is just a shortcut to $('selector').on('click')

elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • 1
    right answer, you shouldn't try to do something different... or the same in both of them. Just always use `.on` – fancy Apr 19 '12 at 09:38
  • it is *now* but until recently `.click()` was a shortcut for `.bind()`. Only since 1.7.x was `.click()` a shortcut for `.on()`. – totallyNotLizards Apr 19 '12 at 09:40
1
$('selector').on('click',function(){ //Do something here }); 

and

$('selector').click(function(){ //Does exact same thing.});

second one short format of first. use any one. first one is suggested

thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
1

As I understand it, as of jQuery 1.7, .click() is merely shorthand for .on('click'). If you want, you can therefore save a function call by using .on('click') directly.

See jQuery source: https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js

Nix
  • 5,746
  • 4
  • 30
  • 51
  • I'm not so sure about it, I've had some problems with dinamically added elements using just click, the event isn't asocciated to the elements, with on('click') I managed to make it work – davidaam Apr 19 '12 at 09:52
-1

You can just create a function, and call it from within both callbacks:

function doSomething()
{
   alert("something");
}

$('selector').on('click',function(){
    // Do some stuff...

    // Do what should be done by both functions
    doSomething();
});
$('otherselector').click(function(){
    // Do some other stuff...

    // Do what should be done by both functions
    doSomething();   
});

Edit based on the comments

If both callbacks should do the exact same thing, you can just pass the function as a callback:

function doSomething()
{
   alert("something");
}

$('selector').on('click',doSomething);
$('otherselector').click(doSomething);

But in that case you could just as well do:

$("selector, otherselector").click(function()
{
   alert("something");
});
Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103
  • Why the downvote? I believe OP wants to do the same thing from two different callbacks. The question is not about whether `.on()` and `.click()` is the same thing. – Christofer Eliasson Apr 19 '12 at 09:40
  • I didnt downvote but I guess its because they're the SAME event no need to do all this. – elclanrs Apr 19 '12 at 09:42
  • @elclanrs That is only true if you want to do the exact same thing for both callbacks. – Christofer Eliasson Apr 19 '12 at 09:43
  • +1 to offset annoying downvotes. however answer isnt perfect - IMO you should point out that using both these events might lead to duplicate function calls if you're not careful (although I notice you thought of that as far as the code goes) – totallyNotLizards Apr 19 '12 at 09:44
  • The OP does want to do the exact same thing. It says so in the question. – JJJ Apr 19 '12 at 09:45