views:

55

answers:

3

Hallo all. I have a little problem.

I have a function. Let’s call it :

function rowClick ()
{

}

Anyway, the question is:

How can I bind this function to a click event without it being called?

What I mean is, if I do this:

$("#holder").click (function(){ rowClick(); });

the rowClick function gets called while registering it to the click event.

Anyway I understand why this happens. I just don’t understand how I can bind the function to the event in a way that it won’t be called.

Thanks.

A: 

How about...

$("#holder").click (rowClick); 

...although the anonymous function should not be invoked (and, therefore, neither should rowClick) until the click event occurs.

belugabob
lol thats what i thought but now when you said it i found out i have a bug.for some reason when i later click on a row its being triggered a few times not just once...
guy schaller
+3  A: 

Remove the function() and parenthesis from the function name, ex:

$("#holder").click(rowClick);
Paul Mrozowski
forgot to mention i have parameters i need to pass to that function..how do i do that without the parentetis?
guy schaller
Create a second parameterless function that calls the other function with the correct parameters.
Paul Mrozowski
Then you should revert to the anonymous function technique - which should work. Can you post some more of the surrounding code?
belugabob
Paul - that would work, but it seems like overkill to me, as the original code should work fine.
belugabob
here it is:"fnRowCallback": function (nRow, aData, iDispalyIndex) { var currRow = $(nRow); currRow.attr("docId", aData[1]); currRow.click(function () { alert("here"); DocGridRowClick(this, aData[1]); }); return nRow; }i use the datatables plugin and that its fnrowcallback
guy schaller
found the problem. the problem was that fnRowCallbackis being called after each cell in the row or something like that.its being called a few times for each row.then it assigned my function manytimes and that why i thought it is being called
guy schaller
That's not how you hook-up to the fnRowCallback (unless you are attempting to mimick what this does via another link?) - this is it's own function callback separate from the click() provided in jQuery (that is, you don't need jQuery's click at all). See this link for details to do what you want: http://www.datatables.net/usage/callbacks - click on the Show Details link.
Paul Mrozowski
So, next time you post a question, could you make it even remotely resemble the actual code? Glad you've solved your problem though.
belugabob
A: 

You just have to pass a function reference as parameter. That does not neccesarily mean an anonymous function.

The call

$("#holder").click ( rowClick );

will work just fine. Don't forget to redesign your rowClick function with an event parameter:

function rowClick (event) {
   alert(event.target.id);
}

This parameter is passed in automatically and can be accessed within your callback function.

jAndy