1

for some reason the initialise function isn't being called when I call my jQuery plugin, this seems to work as expected on another plugin I have created.

Plugin:

(function($) {
    $.fn.youtubeVids = function( options ) {
        var settings = $.extend({
      num: 1,
      id: 'UCeNOYP-TQK-3P3JZerCjheQ',
      elm: null
    }, options );

        function initialize() {
            console.log('dsad');
      $.ajax({
            url: "channel-videos.php",
            data: { num: settings.num, id: settings.id },
            success: function(data) {
                if (settings.elm !== null) {
                    console.log("test");
                } else {
                    console.log("uh oh");
                }
            },
            dataType: "json"
        });
        }
    };
}(jQuery));

Call to the plugin:

<script src="/js-src/youtube-min.js"></script>
<script>
$("#vid1").youtubeVids({
  num: 3,
  elm: [ "#vid1", "#vid2", "#vid2" ]
});
</script>

1 Answer 1

2

The initialize function inside your youtubeVids function is never getting called, so basically nothing happens there (besides creating the settings variable and the initialize function.

You should call the initialize function inside:

(function($) {
  $.fn.youtubeVids = function( options ) {
    var settings = $.extend({
      num: 1,
      id: 'UCeNOYP-TQK-3P3JZerCjheQ',
      elm: null
    }, options );

    function initialize() {
      console.log('dsad');
      $.ajax({
        url: "channel-videos.php",
        data: { num: settings.num, id: settings.id },
        success: function(data) {
          if (settings.elm !== null) {
            console.log("test");
          } else {
            console.log("uh oh");
          }
        },
        dataType: "json"
      });
    }
    initialize();
  };
}(jQuery));

$("#vid1").youtubeVids({
  num: 3,
  elm: [ "#vid1", "#vid2", "#vid2" ]
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="vid1"></div>

Sign up to request clarification or add additional context in comments.

2 Comments

See I thought this but looked in my other plugin and I never call the function in there but it works, at least I cant find the initialisation
There are several ways to do so. Which plugin are you referring to?

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.