2

In jQuery UI, widgets are created like this:

$("#selector").accordion({ //options });

How would I go about creating my own widget that would be created like this? Mainly, I need to know how to define a function that can be called on a DOM element (I'm a javascript newbie and the jQuery UI code isn't so readable).

0

2 Answers 2

2

worlds smallest jQuery plugin! jsfiddle

$.fn.myFunc = function(){
    alert($(this).length)
};
$('div').myFunc();

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

2 Comments

+1 for compactness. It's still good to point out that this is not considered as a best practice. Namespacing, defaults etc. should also be handled correctly. docs.jquery.com/Plugins/Authoring
Very straight to the point. It really gave me a feel of how things are declared.
1

Learn how to create a jQuery plug-in. It's pretty great for organizing your code and makes it easier to reuse functionality.

1 Comment

This is exactly the kind of documentation I was looking for. Thanks a lot!

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.