Solved

First jQuery extension assistance

Posted on 2013-11-13
2
193 Views
Last Modified: 2013-12-05
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
0
Comment
Question by:tonelm54
  • 2
2 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
Comment Utility
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

0
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
Comment Utility
I saw that you're planning to use options also.
Here's an example that uses it:
http://jsfiddle.net/BvssN/1/
(function ($) {
    $.fn.tcDialogue = function (options) {
        var defaultOptions = {
            name: 'John Doe'
        };
        var options = $.extend({}, defaultOptions, options);
        
        this.append("hello " + options.name);
        
        var closeDialogue = function (other_args) {
            alert("I would close if I was written");
        };
        
        return {
            closeDialogue: closeDialogue
        };
    };
}(jQuery));

$(document).ready(function () {
    var testDialogue = $('div').tcDialogue();
    testDialogue.closeDialogue();
    var testDialogue = $('div').empty().tcDialogue({ name: 'Maria' });
});

Open in new window

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now