Avatar of tonelm54
tonelm54
 asked on

First jQuery extension assistance

I'm fed up of writing the same code over and over again, so trying to get to grips with doing my own jQuery extension to simplify and replace some of the most common repetitions.

So after reading a few webpages, a few chapters, and asking a few questions on here, Ive come up with the following code:-
	(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


Which should (as far as I'm aware) put 'hello' into the body, and then display an alert, however all I get from my browser is 'tcDialogue' is not declared, so I'm obviously doing something wrong, but does anyone have any ideas as to what I've done wrong?

Thank you in advance
jQuery

Avatar of undefined
Last Comment
Alexandre Simões

8/22/2022 - Mon
Alexandre Simões

This is what you might be looking for:
http://jsfiddle.net/BvssN/

(function ($) {
    $.fn.tcDialogue = function (options) {
        this.append("hello");
        
        var closeDialogue = function (other_args) {
            alert("I would close if I was written");
        };
        
        return {
            closeDialogue: closeDialogue
        };
    };
}(jQuery));

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

Open in new window

ASKER CERTIFIED SOLUTION
Alexandre Simões

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck