title: Close div and change the button class jquery
body: <p>My div opens with a button and that button has two classes (active or not), I want to change the class when I click on the boby and that works, but when I click again on the button the class is misted.</p>
  
  <p>I've searched a lot but I just found to change the class or close the div on body click, can you help me to combine the two of them?</p>
  
  <p>Please, Thanks!</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;meta http-equiv="Content-Type" content="text/html;charset=utf-8" /&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;title&gt;TEST&lt;/title&gt;
  &lt;script type="text/javascript" src="jquery-1.7.1.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;style&gt;
  body{background:#1E1E1E;}
  .link{background:gray;color:black;}
  .link_active{background:green;color:white;}
  #content{background:gray;border:1px solid;width:100px;height:100px;display:none;}
  &lt;/style&gt;
  &lt;script type="text/javascript"&gt;
  $(document).ready(function(){
      $(".link").toggle(function(){ 
          $(this).toggleClass("link_active");
      },
      function () {
          $(this).removeClass("active");
      });
      $(".link").click(function(){
          $(this).next("#content").slideToggle("fast");
          $(this).removeClass("active");
      });
      $(document).on('click', function() {
          $('#content').css("display","none");
          $('.link').removeClass("link_active");
          $('.link').addClass("link");
      });
  });
  &lt;/script&gt;
  &lt;body&gt;
  &lt;a href="#" class="link"&gt;OPEN&lt;/a&gt;
  &lt;div id="content"&gt; content content content content content content&lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>