views:

72

answers:

6

hi

I am defining function func1(); I want it to take some param(s) ie

var func1(aaa, bbb){
//do something, anything!
};

then i want to call it later in the doc something like this:

$('#some_id').click(func1($this));

but its not working.

I've been messing with it for a while but its very strange the way jquery/javascript handles user defined functions to me at least. Can some1 provide me with simple code snippet? Thx

+1  A: 

It should be

function func1(aaa, bbb)  { ... }
dark_charlie
+1  A: 

Here's a quick example:

Make sure you define your function with function. Then when you want to call it from within jQuery, you should use an inline function within click().

<div>Click Me!</div>
<div>Pick this one!</div>
<script type="text/javascript">   
function func1(aaa, bbb) 
{ 
    alert('You clicked the DIV that said: "' + aaa.innerHTML + '" and ' + bbb); 
};          
$('div').click(function ()
{
    func1(this, "that's ok.");
});
</script>        
Peter Ajtai
+3  A: 

If all you want to pass is $(this), you don't need the parameter, as it will be available inside the defined function.

$('#some_id').click(func1);

However, you can't add parameters like this:

$('#some_id').click(func1(param1, param2));

Then, for your function definition, you just have

function func1() { }

If you do want parameters other than $(this), you will need to do the following:

$('#some_id').click(function () {
    func1(aaa, bbb); });
phoffer
The value of `this` will not reference the element when you call `func1` like that.
patrick dw
@patrick, I hope you are not referring to `$('#some_id').click(func1);` cause it does..
Reigel
@Reigel - I should have been more clear. When I said "call `func1` like that" I meant calling it from inside the anonymous function in the last example. I think of the first example as *passing* `func1`.
patrick dw
It does work, I am using it in a project of mine. Try putting an attribute like name, class, id, etc, and then try to access it in the function by `$(this).attr('name')` and I guarantee it will work. EDIT: I see what you mean by your answer. I don't think any of us know exactly what the poster is trying to do, but I bet he wants to pass element attributes, because there would not really be anything else to pass, because he could just access variables directly.
phoffer
To be clear, `this` will not refer to the element receiving the event in the *last* example given. It will in the first.
patrick dw
clear enough patrick...
Reigel
To prevent all the critics, I meant to add "most of the time" at the very end of my previous comment--so please, don't jump all over it because of global or local variables. I understand the difference, please don't go crazy with that.
phoffer
@phoffer - Agreed, the question isn't perfectly clear. You may be right about the element attributes. If that's the case, then your first example would be the way to go. There are occasions though to pass arguments other than element attributes.
patrick dw
@Reigel - I don't want you to think I was targeting that last clarification at you. I know you knew the difference. Thanks for pointing out the confusion from my first comment. :o)
patrick dw
@patrick: no worries... I was with you on that... :)
Reigel
A: 

You can't do it like $('#some_id').click(func1($this)); because of .click(handler(eventObject)) definition... if you do call it like that, $this in func1($this) would be expected as an eventObject.

for this, you could use .bind() and event.data.

demo

for example..

function sum(event) {
   alert(event.data.x + event.data.y);
   // `this` here would refer to the element calling this function
   // for example, $(this).attr('id') would return `someID` if called below
}

call it as

$('#someID').bind('click',{x:4, y:5}, sum);
Reigel
+4  A: 

I assume you are trying to make sure that this still references the element that received the event.

Try it out: http://jsfiddle.net/jKM9s/

$('#some_id').click(function() {
    func1.call(this, 'somearg', 'somearg'); 
});

   // now "this" references the element when func1 is called
function func1(aaa, bbb){
    //do something, anything!
};

EDIT:

If all you wanted was reference to the element clicked, then see @phoffer's first solution.

patrick dw
@patrick I agree that it just depends on what the OP intends on doing. Unfortunately, he seems to have forgotten to come back to it.
phoffer
@phoffer - Yeah, that happens too often. A group of people eager to help, but no interaction. Probably not accustomed to getting answers so quickly.
patrick dw
@patrick You're exactly right. What I try to do if I ask a question is make sure I'm free for the next hour, just for the purpose of interacting. When everyone works together, this site is flat out amazing.
phoffer
my apologies. I am not used to getting quick replies, next time i will ask question and keep an eye on the thread.Thanks for help ;)
GoTTimw
@GoTTimw - Not a problem. :o) This is a pretty unique site. You'll often get answers within a few minutes.
patrick dw
A: 

In Javascript, you can't assign a function and pass arguments at the same time. Use an anonymous function...

$('#some_id').click(function() {
  var arg1 = $(this);
  var arg2 = 'whatever';

  return func1(arg1, arg2);
});

If the only parameter you need is this, assign the function reference...

$('#some_id').click(func1);
Josh Stodola
I don't know what's the best use for the return there... I guess returning true or false?
Reigel
Returning `false` cancels the click event. Useful for when you need to do validation (prevent a redirect).
Josh Stodola