views:

807

answers:

2

I'm trying to toggle a UI Button from a callback of a POST operation. The jquery UI Button would either need to have "ui-icon ui-icon-minus" or "ui-icon ui-icon-checked" as a span.class

I tried to toggle - but toggle only removes and adds back a part of the class - it doesn't exchange the whole class. This is where I'm stuck at right now:

$('#toggle-page, a.toggle-page').click(function() {
pageID = $(this).parent('div').attr('id');
$.post(
    "webadmin/pages.toggle.serialize.php", 
    {id : pageID },
    function(data, textStatus, xhr) {
      if ($("#"+pageID+" a#toggle-page span").hasClass('ui-icon ui-icon-minus')) {
            $("#"+pageID+" a#toggle-page span").removeClass('ui-icon ui-icon-minus');
            $("#"+pageID+" a#toggle-page span").addClass('ui-icon ui-icon-check');
          }
      if ($("#"+pageID+" a#toggle-page span").hasClass('ui-icon ui-icon-check')) {
            $("#"+pageID+" a#toggle-page span").removeClass('ui-icon ui-icon-check');
            $("#"+pageID+" a#toggle-page span").addClass('ui-icon ui-icon-minus');
          }
    }
);
});

I know the code above is not the right way... but can someone point me in the right direction? The Id etc. are a bit nested because theres multiple entries that can be edited - hence the pageID stuff.

A: 

This should be closer to what you want. Always cache the return from a jQuery selector if you plan to use it again:

$('#toggle-page, a.toggle-page').click(function() {
  var  $page = $(this).parent('div'),
      pageID = $page.attr('id');
  $.post(
      "webadmin/pages.toggle.serialize.php", 
      {id : pageID },
      function(data, textStatus, xhr) {
        var $span = $page.find('a#toggle-page span');
        if($span.hasClass('ui-icon-minus')) {
          $span.removeClass('ui-icon-minus').addClass('ui-icon-check')
        } else {
          $span.removeClass('ui-icon-check').addClass('ui-icon-minus')
        }
      }
  );
});
Doug Neiner
i'm new to this whole jquery stuff - but it seems that you are mixing in some php? or am I wrong ($ sign infront of the variable...)
Mark Nolan
@Mark, not at all. Whenever you return a jQuery result set, it is common practice to store it in a variable that starts with a `$` so you can tell it apart from flat variables (booleans, strings) and DOM elements. Then, throughout your app, you know `$varname` can accept any jQuery method ( i.e. `$varname.addClass()` ), but `myDiv` cannot and must be used like this: `$(myDiv).addClass()`. Hope that makes sense. Additionally, this answer is more efficient because it looks up the `$page` once, and looks up the `$span` once instead of `$('#' + pageID +...` which tries to find it by ID.
Doug Neiner
Wow - thanks for the great answer! Learnt a lot through this...
Mark Nolan
A: 

I would do it like this:

$('#toggle-page, a.toggle-page').click(function() {
pageID = $(this).parent('div').attr('id');
$.post(
    "webadmin/pages.toggle.serialize.php", 
    {id : pageID },
    function(data, textStatus, xhr) {
      var node = $("#"+pageID+" a#toggle-page span");
      node.toggleClass('ui-icon-minus');
      node.toggleClass('ui-icon-check');
    }
);
});
Toader Mihai Claudiu
Works like a charm! Thanks
Mark Nolan