jQuery not removing “class” attribute name when using removeClass()

I've written a "Favourite" feature (special almost entirely to Stefan Hoth and his reply to a previous article), which is — for the most part — working. However, it's quirky.

Firstly, after visting a page, the favourite button needs pressing twice before it begins to work, after which it works with a single click to switch between states.

Secondly, the removeClass() method only appears happy to remove the "active" part of the class="active" attribute, which is weird.

function favouriteAdd (){
    $.ajax({
        url: base_url + "bookmarks/jq_set_bookmark_as_favourite/add/" + $("a#favourite").data("bookmark"),
        success: function () {
            $('a#favourite')
            .addClass('active')
            .attr('title', "Remove as Favorite")
            .unbind('click')
            .bind('click', favouriteRemove);
        }
    });
}

function favouriteRemove (){
    $.ajax({
        url: base_url + "bookmarks/jq_set_bookmark_as_favourite/remove/" + $("a#favourite").data("bookmark"),
        success: function () {
            $('a#favourite')
            .removeClass('active')
            .attr('title', "Add as Favorite")
            .unbind('click')
            .bind('click', favouriteAdd);
        }
    });
}

if($('a#favourite').hasClass('active') == true) {
    $('a#favourite').bind('click', favouriteRemove);
} else {
    $('a#favourite').bind('click', favouriteAdd);
}

I'm guessing the solution is elementary for the initiated, but I'm sadly at in the elementary class when it comes to jQuery.

Answers


Try this but be careful as this will erase the entire class attribute and if you have additional classes in there they will be wiped:

$('a#favourite').removeAttr("class");

Also, try putting your if statement in an ajaxComplete function

$(document).ajaxComplete(function() {
   // code here
 });

Need Your Help

C compiler's pre/post incrementation evaluation in expressions

c visual-c++ gcc expression evaluation

Today I found something, that made me very anxious about my C++ or basic programming skills. The problem is C++ expression evaluation with post/pre incrementation.

Program will not switch to activity on my phone

android

public class TestCamera extends Activity implements SurfaceHolder.Callback, View.OnClickListener {

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.