avoiding object has no method errors

avoiding object has no method errors

Hi All,

I am trying to use some really simple jquery in a rails 3 app. I am trying to get a function to run whenever a rails 3 js callback is fired, the function is to add or remove css classes from link elements. But so far I keep getting errors in the console (chrome) stating that the object defined by my css selectors doesn't have the addClass and removeClass methods.

Here is my code:

  1. $(function($) {
  2. var resetArtistTabStates = function () {
  3. $("#artist-tabs li a".addClass("current"));
  4. alert("something is happening");
  5. };
  6. $("#artist-tabs").bind("ajax:success", resetArtistTabStates);
  7. });

The ajax:success event come from rails.js jquery file, which contains the following:

  1. callRemote: function () {
  2.             var el      = this,
  3.                 method  = el.attr('method') || el.attr('data-method') || 'GET',
  4.                 url     = el.attr('action') || el.attr('href'),
  5.                 dataType  = el.attr('data-type')  || 'script';

  6.             if (url === undefined) {
  7.               throw "No URL specified for remote call (action or href must be present).";
  8.             } else {
  9.                 if (el.triggerAndReturn('ajax:before')) {
  10.                     var data = el.is('form') ? el.serializeArray() : [];
  11.                     $.ajax({
  12.                         url: url,
  13.                         data: data,
  14.                         dataType: dataType,
  15.                         type: method.toUpperCase(),
  16.                         beforeSend: function (xhr) {
  17.                             el.trigger('ajax:loading', xhr);
  18.                         },
  19.                         success: function (data, status, xhr) {
  20.                             el.trigger('ajax:success', [data, status, xhr]);
  21.                         },
  22.                         complete: function (xhr) {
  23.                             el.trigger('ajax:complete', xhr);
  24.                         },
  25.                         error: function (xhr, status, error) {
  26.                             el.trigger('ajax:failure', [xhr, status, error]);
  27.                         }
  28.                     });
  29.                 }

  30.                 el.trigger('ajax:after');
  31.             }
  32.         }

Any ideas why my code is resulting in these errors?

Thanks,
Sean