tags:

views:

82

answers:

5

please tell me what is the problem in this script

$("#mydiv").click(function() 
    {
        console.log("something");
    }, function()
    {
        console.log("something");
    });

it doesn't make second console.

Thanks

+3  A: 

click only takes one function. What are you expecting out of the second function?

To have the second function run after the first function, just put it at the end of the first function. If the first function does something asynchronously, then you'll need to attach your second function as a callback to the first's result.

$('#mydiv').click(function () {
    console.log('first something');
    console.log('second something');
});

or maybe like:

$('#mydiv').click(function () {
    $(this).fadeOut(function () {
        console.log('second something');
    });
});
bdukes
i need function two to run after function1, not the same time, is it possible?(ie. if in function1 i have something which takes 500ms, i need function two to run after 500ms, but without using `setTimeout`...)
Syom
A: 

I don't think you can bind two functions on one event like this. Unless it is designed for it, like hover for example.

PJP
and what .hover does is bind two two other events to the two functions.
Mark Schultheiss
Exactly, it is just a shortcut.
PJP
A: 

There is nothing here to execute the second anonymous (unnamed) function.

EDIT: IF what you want is to bind the two actions of a "click" you can bind the .mousedown and .mouseup functions instead of the singlular .click

EDIT2: Based on comments, produce a private, sequence of execution functions:

$('#mybutton').click(function(){
    var one = function(){
        alert('one');
    };
    var two= function(){
        alert('two');
    };
    one();
    two();
});

To see this in action visit: http://jsfiddle.net/R7aT3/

Mark Schultheiss
+1  A: 

As mentioned above click takes only one handler at time. I guess you need to call it twice:

$("#mydiv").click(function() 
{
    console.log("something 1");
});
$("#mydiv").click(function()
{
    console.log("something 2");
});
Ivan Nevostruev
+2  A: 

You can probably just write your two functions as actual functions, then call each in order from your anonymous functions. Like this:

function a()
{
  console.log("something");
}

function b()
{
  console.log("something");
}

$("#id").click(function(){
  a();
  b();
});
bigrockshow
Simple and clear solution.
Ivan Nevostruev