4

If I have a function:

function doSomething() {
   ///
}

I can use this with .change as follows:

$("input[name='bla']").change(doSomething);

However, what if the function was:

function doSomething(bla) {
   ///
}

And I wanted to pass "this" as a parameter to the function? This doesn't seem to work:

$("input[name='bla']").change(doSomething(this));

Any ideas?

UPDATE

I do not want this:

$('a.link').change(function (e) { doSomething($(this)); } );

I want this: (but the correct syntax that will work)

$('a.link').change( doSomething($(this)) );
3
  • what you are trying to do this on doSomething and in which context its not working. Commented Dec 10, 2013 at 9:38
  • I'm trying to pass a reference to the selected radio button to the function I created Commented Dec 10, 2013 at 9:40
  • 1
    Check these links - call a function on click event call a function on change event Commented Dec 10, 2013 at 9:46

5 Answers 5

5

http://api.jquery.com/change/ and all other jquery events have a variation of the event handler that accepts eventData as the first param.

.change( [eventData ], handler(eventObject) )

But in this case your handler function would look for eventData in the eventObject

http://api.jquery.com/event.data/

So your function would look something like this to use this variation.

var that = this;
$("input[name='bla']").change({otherThis:that}, doSomething);

function doSomething(e) {
   var $this = $(this);
   var otherThis = e.data.otherThis;
}

I added

var $this = $(this);

to show that

$this

is the

$("input[name='bla']")

that was changed. Hope that helps.

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

Comments

0

What about What about

$("input[name='bla']").change(doSomething($(this)));

I think you can refer objects using $(this) in jquery.

Comments

0

jQuery automatically bind the function call with this. You don't have to pass $(this) on. Only if you want to parse the context of its parent to the function.

For example:

$('a.link').change(function (a) {
     console.log(a); // is event object
     console.log($(this)); // is the same as one match of $('a.link'). `this` is binded to the element 
});

(after comment) Or else you can try:

$('a.link').change(function (e) { doSomething($(this)); } );

2 Comments

Can you explain why you want to do that? Usually there is no need.
I want to figure out what the format must be in order to call the function with a parameter, without including the function(e) part.
0

Using jQuery you refer to this using $(this) , but pay attention it will be passed as an object !!

Comments

0

The usual solution is to use a variable in the closure scope :

var that = this;
('a.link').change(function (e) { doSomething($(that)); } );

Comments

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.