Solved

First jQuery extension assistance

Posted on 2013-11-13
2
200 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
ID: 39644288
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
ID: 39644494
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
popup load of page and setting up of session 8 37
jquery - show / hide div depending on day and time 19 46
jquery conditionally populate list boxes 2 31
Button to go back 3 25
by Julian Matz As of jQuery (http://jquery.com/) 1.4 the .live() method (http://api.jquery.com/live/) supports custom events as well as some standard JavaScript events that it previously didn't support. Among these is the submit event. Unlike …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

776 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