• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

Jquery building function

I've been looking at creating my own function, and although I've managed to pass parameters into my function with success, however I'm confused as to how to add functions to my function and how to make the function call with my code.

I've been looking at tutorials but cant seem to any tutorials about how to add functions, and how to call them in my code creation.

Does anyone have any tutorials that can help? Or any suggestions, or code examples?

Thank you
0
tonelm54
Asked:
tonelm54
  • 2
1 Solution
 
OmniUnlimitedCommented:
<competing site link removed - GaryC123>

(function($) { 
    $.fn.foo = function(options) {
        // whatever $().foo() should do

        var do_stuff = function(do_stuff_args) {
            console.log("hello world!");
            // do stuff with options and do_stuff_args

            var do_other_stuff = function(other_args) {
                alert("who are you?");
                // here you have access to options, do_stuff_args, and other_args
            };

            return {
                do_other_stuff: do_other_stuff
            };
        };

        return {
            do_stuff: do_stuff
        }
    };
})(jQuery);

Open in new window


And call it using:

foo().do_stuff(some_options).do_other_stuff(other_options);or

var a = foo(stuff).do_stuff(some_options);
a.do_other_stuff(foo);
a.do_other_stuff(bar);

Open in new window

0
 
tonelm54Author Commented:
Ok, so from what I understand and from what you have said, I have written:-
	(function( $ ) {
		$.fn.tcDialogue = function(options) {
			$('body').append("hello");
			
            var closeDialogue = function(other_args) {
                alert("I would close if I was written");
            };
		};
	}( jQuery ));

	$(document).ready(function (){
		var testDialogue = $.tcDialogue();
		testDialogue.closeDialogue();
    });

Open in new window


However I keep getting an error saying that tcDialogue();
0
 
OmniUnlimitedCommented:
No, you have your var declared wrong.  It should read:

var testDialogue = $tcDialogue();

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now