1

I'm creating a JQuery plugin which takes in parameter a String : handler.

Depending on that, my plugin will be launched. Example if : "handler = submit"

In my plugin I'll call :

$("form").submit(function() {
// Code inside
});

With handler = "focusOut"

$("form").submit(function() {
// Code inside
});

I've tried :

 $("form").this[handler](function()
 $("form").window[handler](function() 

But no results. Any idea ? Thanks for the help :)

1 Answer 1

3

If it's an event

var handler = 'submit';

$("form").on(handler, function() {

});

If it's a jQuery method

var handler = 'hide';

$('#element')[handler]();

or with a callback

var handler = 'fadeIn';

$('form')[handler](function() {

});

$() returns an object, so you can use either dot or bracket notation to reference the method, which would be a property of the object etc. so just replace the dot notation with the appropriate bracket notation instead.

If it's an event, you're better of using on(), but you could also use bracket notation to reference the method directly, like above.

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

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.