0

I'm struggling to get my head around the way I author (and attach to events) in a custom jQuery plugin.

I have the start of a plugin, in this simple example....

http://jsfiddle.net/ETFairfax/mRAHF/9/

(function($) {
    $.fn.myPlugIn = function(options) {
        var defaults = {
            'width': 200,
            'url': 'www.someurl.com'
        };
        // If options exist, lets merge them with our default settings
        var options = $.extend({}, defaults, options);

        function somePrivateFunction() {
            return options.url;
        }

        return this.each(function() {
            $(this).text(somePrivateFunction());
            $(this).width(options.width);
        });
    };
})(jQuery);

$(document).ready(function() {

    $('#testDiv1').myPlugIn({
        'width': 150
    });
    $('#testDiv2').myPlugIn({
        'url': 'www.google.com'
    });
});

<div id="eventDriver1">Click here to update 1</div>
<div id="eventDriver2">Click here to update 2</div>
<div id="testDiv1" class="testClass"></div>
<div id="testDiv2" class="testClass"></div>

As you can see I set testDiv1, and testDiv2, and they behave as I'd expect.

Now I want to be able to say that eventDriver1 should refresh testDiv1, and for it to specify what text shoudl go into it. I just don't know where to start!!

I appreciate that will have been documented somewhere, and/or asked already, but I'm just not sure what buzzwords I need to search for!!! Any help appreciated.

Thanks.

2
  • 1
    Binding events in jQuery is accomplished with .bind() or .live() or by using one of the shortcuts (such as .click(), .submit(), .load(), etc). I'm not really sure what your question is... Commented May 17, 2011 at 19:01
  • Please include your code in the question. Thanks. Commented May 17, 2011 at 19:03

2 Answers 2

1

How about this:

Live Demo

$('#eventDriver1').click(function() {
    $('#testDiv1').myPlugIn({'url': 'www.yahoo.com'});
});
$('#eventDriver2').click(function() {
    $('#testDiv2').myPlugIn({'url': 'www.microsoft.com', 'width': 250});
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the example, and although it works just fine, and I'm happy to use that method if its the way to do it, but I'm trying to work out how to expose some sort of Update() method on the plugin, and how to call it. Maybe I have the wrong idea of how things tie together?
1

Use click.

$('#eventDriver1').click(function(){/*do something*/})

1 Comment

Thanks for the reply, but it's the /* Do Something */ bit that I am confused about. I'm trying to work out how to expose some sort of Update() method on the plugin, and how to call it? Does that make sense?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.