0

i have this function in my js file

(function($) {
  $.fn.foobar = function() {
   // some code 
  };
})(jQuery);

and when i call the following

jQuery(function() {
  $.getScript("http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js");
  $.getScript("http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js");
     jQuery('object').foobar(); 
}); 

it gives me the following

Uncaught TypeError: Object [object Object] has no method 'foobar'

i'm trying this on Rails file (*.js.erb)

Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
Mohamed Emad Hegab
  • 2,665
  • 6
  • 39
  • 64

3 Answers3

0

I guess you are overwriting your jQuery array with that second one, try doing this:

(function($) {
    $.fn.foobar = function() {
        console.log(true);
    };
})(jQuery);

(function($) {
            $.getScript("http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js", function() {
            $('body').foobar();
        }); 
})(jQuery);

working fiddle

Alex
  • 9,911
  • 5
  • 33
  • 52
0

Only run these codes in the fiddle, it runs fine. So, i guess that your code may be have some error at other place.I suggest you show more code for the question.

0

try something like this

<script src="jquery.js">
<script>
    (function($) {
      $.fn.foobar = function() {
       // some code 
      };
    })(jQuery);
    $(function(){
        jQuery('object').foobar(); 
    })
</script>
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40