views:

28

answers:

2

I'm writing the wrong syntax, so the function is not getting called.

I'm making a jQuery function that just sends an AJAX call and redirects. But it doesn't actually apply to any selector.

my function

$.fn.update_and_return = function() {
  $.ajax({type: "GET",
          beforeSend: function(){
            idx_var = $(".selected_adli").prevAll().length;
            }, 
          url: '#{organization_media_gallery_path(@organization, @gallery)}', 
          dataType: "script",
          complete: function(){
            $(".opened_photo").fadeOut(function(){
              $(".adli").eq(idx_var - 1).addClass("selected_adli");
              $(".media_lib").fadeIn();
            });
          }
    });
  }

How I instantiate it :

$.update_and_return();

Doesn't work. Why is that?

+1  A: 

Hers is an Example for doing it with an Object and an Function alone (with jQuery) Hope it helps:

$.someobject = {
   somevar: "someval",
   somemethod: function(){ alert('Baazinga'); }
};

$.somefunction = function(){ alert('Baazonga'); };

$(document).ready(function(){
        $.someobject.somemethod();
        $.somefunction();
});
Hannes
+1  A: 

$.fn.function_name adds a function to the object returned by a call to $('#css_selector').

If you want to attach a function to the jQuery object $, you can just do that:

$.function_name = function() { /* Stuff */ } ;
Gus