Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7404705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:17:38+00:00 2026-05-29T05:17:38+00:00

I have created a jQuery plugin that grabs a JSON feed (in this case

  • 0

I have created a jQuery plugin that grabs a JSON feed (in this case YouTube). It then displays the results in a DIV – everything is fine until: I want to show the results (with different settings: i.e. more videos, other channel) in two or more containers – how can I achieve this?

btw… Is this the right title for my question? I’m by far not a jQuery/JS expert 🙂

Here is my code so far – I shortened it to the fundamental actions and a rudimentary output:

(function($){
    // Default settings
    var settings = {
        author:     'ARD',
        results:    3
    };

    // Append to Container
    var append_to_container;

    // The Methods
    var methods = {
        // Initate
        init: function(options){
            append_to_container = $(this);

            $.extend(settings, options);
            methods.grabFeed();
        },

        // Grab feed
        grabFeed: function(){
            $.ajax({
                url: 'http://gdata.youtube.com/feeds/api/videos',
                data: {
                    author: settings.author,
                    v: '2',
                    alt: 'jsonc',
                    'max-results': settings.results
                },
                dataType: 'jsonp',
                success: function(data){
                    methods.gotResults(data);
                }
            });
        },

        // Placeholder for results
        gotResults: function(data){
            console.log(data);
            $("<h1>Success!</h1>").appendTo(append_to_container);
        }
    };


    $.fn.youtubeVideos = function(method){
        // Method calling logic
        if ( methods[method] ) {
            return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.youtubeVideos' );
        }
    }

})(jQuery);

Now when I call $('#main').youtubeVideos(); it works as expected.

But now I want to show the results in more than one DOM element. So when I call for example:

$('#main').youtubeVideos();
$('#same_videos').youtubeVideos();

it show the Success! message two times in the container used in the second call.

I tried with several answers from stackoveflow and the web – but I can’t find a way to use my function more than one time. How can I use my plugin more than only one time on several containers?


EDIT / Solution

Here is my solution after the tip from Codemonkey:

(function($){

    $.fn.youtubeVideos = function(method){

        // Default settings
        var settings = {
            author:     'ARD',
            results:    3
        };

        // Append to Container
        var append_to_container;

        // The Methods
        var methods = {
            // Initate
            init: function(options){
                append_to_container = $(this);

                $.extend(settings, options);
                methods.grabFeed();
            },

            // Grab feed
            grabFeed: function(){
                $.ajax({
                    url: 'http://gdata.youtube.com/feeds/api/videos',
                    data: {
                        author: settings.author,
                        v: '2',
                        alt: 'jsonc',
                        'max-results': settings.results
                    },
                    dataType: 'jsonp',
                    success: function(data){
                        methods.gotResults(data);
                    }
                });
            },

            // Placeholder for results
            gotResults: function(data){
                console.log(data);
                $("<h1>Success!</h1>").appendTo(append_to_container);
            }
        };


        // Method calling logic
        if ( methods[method] ) {
            return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.youtubeVideos' );
        }
    }

})(jQuery);

$('#main').youtubeVideos();
$('#same_videos').youtubeVideos();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:17:38+00:00Added an answer on May 29, 2026 at 5:17 am

    By putting all your functionality into:

    $.fn.youtubeVideos = function(method){ ...
    

    That’s it. The context of that function will be unique for every time you run the plugin function. If you need data to persist in a jQuery object over multiple calls of the plugin function then use jQuery.data. Other than that you’re good to go.

    You should check out some jQuery plugin examples (Pick and choose) to see how they work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this jQuery ajax navigation tabs plugin that I created using some help
I have created a jQuery elastic plugin. I have the problem that if I
I have a jQuery tooltip plugin that displays a tooltip when I hover over
in a jQuery plugin i have created helper functions, like this (function($) { var
I have created a jQuery plugin that works great with the exception of being
I am using the jQuery Tools Scrollable plugin - http://flowplayer.org/tools/scrollable.html#navigator I have created a
I have created a jQuery animation that can be seen by clicking the Preview
I have created a dynamic list picker script using Jquery 1.3 and PHP that
I have created a drop down with jQuery that can be seen here by
I have created a dialog with the JQuery dialog plugin. My dialog is defined

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.