Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Jquery building function

Posted on 2013-11-12
3
214 Views
Last Modified: 2013-12-05
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
Comment
Question by:tonelm54
  • 2
3 Comments
 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 39642540
<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
 

Author Comment

by:tonelm54
ID: 39643982
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
 
LVL 17

Accepted Solution

by:
OmniUnlimited earned 500 total points
ID: 39648047
No, you have your var declared wrong.  It should read:

var testDialogue = $tcDialogue();

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Lazy load not working with masonry js 1 22
Jquery GMAP3 Hierarchy 1 35
jquery auto complete 11 20
show form submit button based on jquery sum 5 45
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question